github.com/square/finch@v0.0.0-20240412205204-6530c03e2b96/data/id_test.go (about) 1 // Copyright 2024 Block, Inc. 2 3 package data_test 4 5 import ( 6 "testing" 7 8 "github.com/go-test/deep" 9 10 "github.com/square/finch" 11 "github.com/square/finch/data" 12 ) 13 14 func TestXid_TrxScope(t *testing.T) { 15 g := data.NewScopedGenerator( 16 data.Id{ 17 Scope: finch.SCOPE_TRX, 18 Type: "xid", 19 DataKey: "@d", 20 }, 21 data.NewXid()) 22 23 r := data.RunCount{} 24 r[data.TRX] = 1 25 26 v1 := g.Values(r) 27 v2 := g.Values(r) 28 29 if len(v1) != 1 || len(v2) != 1 { 30 t.Fatalf("%d and %d values, expected 1 value from each call to Values()", len(v1), len(v2)) 31 } 32 33 if v1[0].(string) != v2[0].(string) { 34 t.Errorf("different values for same trx, expect same: %s != %s", v1, v2) 35 } 36 37 // Next trx should cause new value 38 r[data.TRX] += 1 39 v3 := g.Values(r) 40 v4 := g.Values(r) 41 42 if len(v3) != 1 || len(v4) != 1 { 43 t.Fatalf("%d and %d values, expected 1 value from each call to Values()", len(v3), len(v4)) 44 } 45 46 if v3[0].(string) != v4[0].(string) { 47 t.Errorf("different values for same trx, expect same: %s != %s", v3, v4) 48 } 49 50 if v3[0].(string) == v1[0].(string) { 51 t.Errorf("trx 2 values == trx 1 values, expected different values: %s == %s", v3[0].(string), v1[0].(string)) 52 } 53 } 54 55 func TestClientId(t *testing.T) { 56 rc := data.RunCount{ 57 1, 1, 1, 1, // couters 58 5, 6, 7, 8, // client, cg, eg, stage 59 } 60 61 // With default (no params): returns just client id (5) 62 g, err := data.NewClientId(nil) 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 got := g.Values(rc) 68 expect := []interface{}{uint(5)} 69 if diff := deep.Equal(got, expect); diff != nil { 70 t.Error(diff) 71 } 72 73 n, _ := g.Format() 74 if n != 1 { 75 t.Errorf("Format return n=%d, expected 1", n) 76 } 77 78 // With all 3 ids: client, client group, exec group 79 g, err = data.NewClientId(map[string]string{"ids": "client,client-group,exec-group"}) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 got = g.Values(rc) 85 expect = []interface{}{uint(5), uint(6), uint(7)} 86 if diff := deep.Equal(got, expect); diff != nil { 87 t.Error(diff) 88 } 89 90 n, _ = g.Format() 91 if n != 3 { 92 t.Errorf("Format return n=%d, expected 3", n) 93 } 94 }