github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/kafka_test.go (about)

     1  package kafka
     2  
     3  /**
     4   * Copyright 2016 Confluent Inc.
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  //Test LibraryVersion()
    24  func TestLibraryVersion(t *testing.T) {
    25  	ver, verstr := LibraryVersion()
    26  	if ver >= 0x00090200 {
    27  		t.Logf("Library version %d: %s\n", ver, verstr)
    28  	} else {
    29  		t.Errorf("Unexpected Library version %d: %s\n", ver, verstr)
    30  	}
    31  }
    32  
    33  //Test Offset APIs
    34  func TestOffsetAPIs(t *testing.T) {
    35  	offsets := []Offset{OffsetBeginning, OffsetEnd, OffsetInvalid, OffsetStored, 1001}
    36  	for _, offset := range offsets {
    37  		t.Logf("Offset: %s\n", offset.String())
    38  	}
    39  
    40  	// test known offset strings
    41  	testOffsets := map[string]Offset{"beginning": OffsetBeginning,
    42  		"earliest": OffsetBeginning,
    43  		"end":      OffsetEnd,
    44  		"latest":   OffsetEnd,
    45  		"unset":    OffsetInvalid,
    46  		"invalid":  OffsetInvalid,
    47  		"stored":   OffsetStored}
    48  
    49  	for key, expectedOffset := range testOffsets {
    50  		offset, err := NewOffset(key)
    51  		if err != nil {
    52  			t.Errorf("Cannot create offset for %s, error: %s\n", key, err)
    53  		} else {
    54  			if offset != expectedOffset {
    55  				t.Errorf("Offset does not equal expected: %s != %s\n", offset, expectedOffset)
    56  			}
    57  		}
    58  	}
    59  
    60  	// test numeric string conversion
    61  	offset, err := NewOffset("10")
    62  	if err != nil {
    63  		t.Errorf("Cannot create offset for 10, error: %s\n", err)
    64  	} else {
    65  		if offset != Offset(10) {
    66  			t.Errorf("Offset does not equal expected: %s != %s\n", offset, Offset(10))
    67  		}
    68  	}
    69  
    70  	// test integer offset
    71  	var intOffset = 10
    72  	offset, err = NewOffset(intOffset)
    73  	if err != nil {
    74  		t.Errorf("Cannot create offset for int 10, Error: %s\n", err)
    75  	} else {
    76  		if offset != Offset(10) {
    77  			t.Errorf("Offset does not equal expected: %s != %s\n", offset, Offset(10))
    78  		}
    79  	}
    80  
    81  	// test int64 offset
    82  	var int64Offset int64 = 10
    83  	offset, err = NewOffset(int64Offset)
    84  	if err != nil {
    85  		t.Errorf("Cannot create offset for int64 10, Error: %s \n", err)
    86  	} else {
    87  		if offset != Offset(10) {
    88  			t.Errorf("Offset does not equal expected: %s != %s\n", offset, Offset(10))
    89  		}
    90  	}
    91  
    92  	// test invalid string offset
    93  	invalidOffsetString := "what is this offset"
    94  	offset, err = NewOffset(invalidOffsetString)
    95  	if err == nil {
    96  		t.Errorf("Expected error for this string offset. Error: %s\n", err)
    97  	} else if offset != Offset(0) {
    98  		t.Errorf("Expected offset (%v), got (%v)\n", Offset(0), offset)
    99  	}
   100  	t.Logf("Offset for string (%s): %v\n", invalidOffsetString, offset)
   101  
   102  	// test double offset
   103  	doubleOffset := 12.15
   104  	offset, err = NewOffset(doubleOffset)
   105  	if err == nil {
   106  		t.Errorf("Expected error for this double offset: %f. Error: %s\n", doubleOffset, err)
   107  	} else if offset != OffsetInvalid {
   108  		t.Errorf("Expected offset (%v), got (%v)\n", OffsetInvalid, offset)
   109  	}
   110  	t.Logf("Offset for double (%f): %v\n", doubleOffset, offset)
   111  
   112  	// test change offset via Set()
   113  	offset, err = NewOffset("beginning")
   114  	if err != nil {
   115  		t.Errorf("Cannot create offset for 'beginning'. Error: %s\n", err)
   116  	}
   117  
   118  	// test change to a logical offset
   119  	err = offset.Set("latest")
   120  	if err != nil {
   121  		t.Errorf("Cannot set offset to 'latest'. Error: %s \n", err)
   122  	} else if offset != OffsetEnd {
   123  		t.Errorf("Failed to change offset. Expect (%v), got (%v)\n", OffsetEnd, offset)
   124  	}
   125  
   126  	// test change to an integer offset
   127  	err = offset.Set(int(10))
   128  	if err != nil {
   129  		t.Errorf("Cannot set offset to (%v). Error: %s \n", 10, err)
   130  	} else if offset != 10 {
   131  		t.Errorf("Failed to change offset. Expect (%v), got (%v)\n", 10, offset)
   132  	}
   133  
   134  	// test OffsetTail()
   135  	tail := OffsetTail(offset)
   136  	t.Logf("offset tail %v\n", tail)
   137  
   138  }