...

Source file src/github.com/twmb/franz-go/pkg/kversion/kversion_test.go

Documentation: github.com/twmb/franz-go/pkg/kversion

     1  package kversion
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  
     8  func TestSetMaxKeyVersion(t *testing.T) {
     9  	var vs Versions
    10  	for i := int16(0); i < math.MaxInt16; i++ {
    11  		vs.SetMaxKeyVersion(i, i)
    12  	}
    13  	for i, v := range vs.k2v {
    14  		if int16(i) != v {
    15  			t.Errorf("set incorrect: at %d got %d != exp %d", i, v, i)
    16  		}
    17  	}
    18  }
    19  
    20  func TestVersionGuess(t *testing.T) {
    21  	// Cases where last can be empty.
    22  	{
    23  		v := V0_8_0()
    24  		if got, exp := v.VersionGuess(), "v0.8.0"; got != exp {
    25  			t.Errorf("got %s != exp %s without modifications", got, exp)
    26  		}
    27  		v.SetMaxKeyVersion(0, -1)
    28  		if got, exp := v.VersionGuess(), "not even v0.8.0"; got != exp {
    29  			t.Errorf("got %s != exp %s unsetting produce", got, exp)
    30  		}
    31  		v.SetMaxKeyVersion(0, 100)
    32  		if got, exp := v.VersionGuess(), "unknown custom version at least v0.8.0"; got != exp {
    33  			t.Errorf("got %s != exp %s maxing produce", got, exp)
    34  		}
    35  		v.SetMaxKeyVersion(1, -1)
    36  		if got, exp := v.VersionGuess(), "unknown custom version"; got != exp {
    37  			t.Errorf("got %s != exp %s maxing produce and unsetting fetch", got, exp)
    38  		}
    39  	}
    40  
    41  	// In between and into the next version.
    42  	{
    43  		v := V0_9_0()
    44  		if got, exp := v.VersionGuess(), "v0.9.0"; got != exp {
    45  			t.Errorf("got %s != exp %s without modifications", got, exp)
    46  		}
    47  		v.SetMaxKeyVersion(17, 0)
    48  		if got, exp := v.VersionGuess(), "between v0.9.0 and v0.10.0"; got != exp {
    49  			t.Errorf("got %s != exp %s setting sasl handshake to 0", got, exp)
    50  		}
    51  		v.SetMaxKeyVersion(0, 2)
    52  		v.SetMaxKeyVersion(1, 2)
    53  		v.SetMaxKeyVersion(3, 1)
    54  		v.SetMaxKeyVersion(6, 2)
    55  		v.SetMaxKeyVersion(18, 0)
    56  		if got, exp := v.VersionGuess(), "v0.10.0"; got != exp {
    57  			t.Errorf("got %s != exp %s setting api versions to 0", got, exp)
    58  		}
    59  	}
    60  
    61  	// This hits the case where versions are -1.
    62  	{
    63  		v := V2_7_0()
    64  		v.SetMaxKeyVersion(int16(len(v.k2v)+1), -1)
    65  		if got, exp := v.VersionGuess(), "v2.7"; got != exp {
    66  			t.Errorf("got %s != exp %s without modifications", got, exp)
    67  		}
    68  	}
    69  
    70  	{ // Here, we ensure we skip 4, 5, 6, and 7 by default.
    71  		v := V2_7_0()
    72  		v.SetMaxKeyVersion(4, -1)
    73  		v.SetMaxKeyVersion(5, -1)
    74  		v.SetMaxKeyVersion(6, -1)
    75  		v.SetMaxKeyVersion(7, -1)
    76  
    77  		if got, exp := v.VersionGuess(), "v2.7"; got != exp {
    78  			t.Errorf("got %s != exp %s for v2.7 with 4,5,6,7 unset", got, exp)
    79  		}
    80  
    81  		if got, exp := v.VersionGuess(SkipKeys()), "unknown custom version"; got != exp {
    82  			t.Errorf("got %s != exp %s for v2.7 with 4,5,6,7 unset without skipping them", got, exp)
    83  		}
    84  	}
    85  }
    86  
    87  func TestEqual(t *testing.T) {
    88  	l := V2_7_0()
    89  	l.SetMaxKeyVersion(int16(len(l.k2v)+1), -1)
    90  
    91  	r := V2_7_0()
    92  
    93  	if !l.Equal(r) {
    94  		t.Errorf("unexpectedly not equal")
    95  	}
    96  
    97  	l.SetMaxKeyVersion(0, -1)
    98  	if l.Equal(r) {
    99  		t.Errorf("unexpectedly equal after unsetting produce in left")
   100  	}
   101  
   102  	r.SetMaxKeyVersion(0, -1)
   103  	if !l.Equal(r) {
   104  		t.Errorf("unexpectedly not equal after unsetting produce in both")
   105  	}
   106  
   107  	l = V0_8_0()
   108  	r = V0_8_1()
   109  	if l.Equal(r) {
   110  		t.Errorf("unexpectedly equal v0.8.0 to v0.8.1")
   111  	}
   112  
   113  	r.SetMaxKeyVersion(8, -1)
   114  	r.SetMaxKeyVersion(9, -1)
   115  	if !l.Equal(r) {
   116  		t.Errorf("unexpectedly not equal after backing v0.8.1 down to v0.8.0")
   117  	}
   118  	if !r.Equal(l) {
   119  		t.Errorf("unexpectedly not equal after backing v0.8.1 down to v0.8.0, opposite direction")
   120  	}
   121  }
   122  
   123  func TestVersionProbeKafka3_1(t *testing.T) {
   124  	versions := map[int16]int16{
   125  		0:  9,  // Produce
   126  		1:  13, // Fetch
   127  		2:  7,  // ListOffsets
   128  		3:  12, // Metadata
   129  		4:  5,  // LeaderAndISR
   130  		5:  3,  // StopReplica
   131  		6:  7,  // UpdateMetadata
   132  		7:  3,  // ControlledShutdown
   133  		8:  8,  // OffsetCommit
   134  		9:  8,  // OffsetFetch
   135  		10: 4,  // FindCoordinator
   136  		11: 7,  // JoinGroup
   137  		12: 4,  // Heartbeat
   138  		13: 4,  // LeaveGroup
   139  		14: 5,  // SyncGroup
   140  		15: 5,  // DescribeGroups
   141  		16: 4,  // ListGroups
   142  		17: 1,  // SASLHandshake
   143  		18: 3,  // ApiVersions
   144  		19: 7,  // CreateTopics
   145  		20: 6,  // DeleteTopics
   146  		21: 2,  // DeleteRecords
   147  		22: 4,  // InitProducerID
   148  		23: 4,  // OffsetForLeaderEpoch
   149  		24: 3,  // AddPartitionsToTxn
   150  		25: 3,  // AddOffsetsToTxn
   151  		26: 3,  // EndTxn
   152  		27: 1,  // WriteTxnMarkers
   153  		28: 3,  // TxnOffsetCommit
   154  		29: 2,  // DescribeACLs
   155  		30: 2,  // CreateACLs
   156  		31: 2,  // DeleteACLs
   157  		32: 4,  // DescribeConfigs
   158  		33: 2,  // AlterConfigs
   159  		34: 2,  // AlterReplicaLogDirs
   160  		35: 2,  // DescribeLogDirs
   161  		36: 2,  // SASLAuthenticate
   162  		37: 3,  // CreatePartitions
   163  		38: 2,  // CreateDelegationToken
   164  		39: 2,  // RenewDelegationToken
   165  		40: 2,  // ExpireDelegationToken
   166  		41: 2,  // DescribeDelegationToken
   167  		42: 2,  // DeleteGroups
   168  		43: 2,  // ElectLeaders
   169  		44: 1,  // IncrementalAlterConfigs
   170  		45: 0,  // AlterPartitionAssignments
   171  		46: 0,  // ListPartitionReassignments
   172  		47: 0,  // OffsetDelete
   173  		48: 1,  // DescribeClientQuotas
   174  		49: 1,  // AlterClientQuotas
   175  		50: 0,  // DescribeUserSCRAMCredentials
   176  		51: 0,  // AlterUserSCRAMCredentials
   177  		56: 0,  // AlterPartition
   178  		57: 0,  // UpdateFeatures
   179  		60: 0,  // DescribeCluster
   180  		61: 0,  // DescribeProducers
   181  		65: 0,  // DescribeTransactions
   182  		66: 0,  // ListTransactions
   183  		67: 0,  // AllocateProducerIDs
   184  	}
   185  
   186  	var vs Versions
   187  	for k, v := range versions {
   188  		vs.SetMaxKeyVersion(k, v)
   189  	}
   190  	if guess := vs.VersionGuess(); guess != "v3.1" {
   191  		t.Errorf("unexpected version guess, got %s != exp %s", guess, "v3.1")
   192  	}
   193  }
   194  

View as plain text