github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/topic_client_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package integration 5 6 import ( 7 "context" 8 "os" 9 "path" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/require" 14 "google.golang.org/grpc" 15 16 ydb "github.com/ydb-platform/ydb-go-sdk/v3" 17 "github.com/ydb-platform/ydb-go-sdk/v3/config" 18 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" 19 "github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions" 20 "github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes" 21 ) 22 23 const defaultConnectionString = "grpc://localhost:2136/local" 24 25 const commonConsumerName = "consumer" 26 27 func TestTopicCreateDrop(t *testing.T) { 28 ctx := xtest.Context(t) 29 db := connect(t) 30 topicPath := db.Name() + "/testtopic" 31 32 _ = db.Topic().Drop(ctx, topicPath) 33 err := db.Topic().Create(ctx, topicPath, 34 topicoptions.CreateWithConsumer( 35 topictypes.Consumer{ 36 Name: "test", 37 }, 38 ), 39 ) 40 require.NoError(t, err) 41 42 _, err = db.Topic().Describe(ctx, topicPath) 43 require.NoError(t, err) 44 45 err = db.Topic().Drop(ctx, topicPath) 46 require.NoError(t, err) 47 } 48 49 func TestTopicDescribe(t *testing.T) { 50 ctx := xtest.Context(t) 51 db := connect(t) 52 topicName := "test-topic-" + t.Name() 53 54 var ( 55 supportedCodecs = []topictypes.Codec{topictypes.CodecRaw, topictypes.CodecGzip} 56 minActivePartitions = int64(2) 57 // partitionCountLimit = int64(5) LOGBROKER-7800 58 retentionPeriod = time.Hour 59 writeSpeed = int64(1023) 60 burstBytes = int64(222) 61 consumers = []topictypes.Consumer{ 62 { 63 Name: "c1", 64 Important: false, 65 SupportedCodecs: []topictypes.Codec{topictypes.CodecRaw, topictypes.CodecGzip}, 66 ReadFrom: time.Date(2022, 9, 11, 10, 1, 2, 0, time.UTC), 67 }, 68 { 69 Name: "c2", 70 SupportedCodecs: []topictypes.Codec{}, 71 ReadFrom: time.Date(2021, 1, 2, 3, 4, 5, 0, time.UTC), 72 }, 73 } 74 ) 75 76 _ = db.Topic().Drop(ctx, topicName) 77 err := db.Topic().Create(ctx, topicName, 78 topicoptions.CreateWithSupportedCodecs(supportedCodecs...), 79 topicoptions.CreateWithMinActivePartitions(minActivePartitions), 80 // topicoptions.CreateWithPartitionCountLimit(partitionCountLimit), LOGBROKER-7800 81 topicoptions.CreateWithRetentionPeriod(retentionPeriod), 82 // topicoptions.CreateWithRetentionStorageMB(...) - incompatible with retention period 83 topicoptions.CreateWithPartitionWriteSpeedBytesPerSecond(writeSpeed), 84 topicoptions.CreateWithPartitionWriteBurstBytes(burstBytes), 85 topicoptions.CreateWithConsumer(consumers...), 86 // topicoptions.CreateWithMeteringMode(topictypes.MeteringModeRequestUnits), - work with serverless only 87 ) 88 require.NoError(t, err) 89 90 res, err := db.Topic().Describe(ctx, topicName) 91 require.NoError(t, err) 92 93 expected := topictypes.TopicDescription{ 94 Path: topicName, 95 PartitionSettings: topictypes.PartitionSettings{ 96 MinActivePartitions: minActivePartitions, 97 // PartitionCountLimit: partitionCountLimit, LOGBROKER-7800 98 }, 99 Partitions: []topictypes.PartitionInfo{ 100 { 101 PartitionID: 0, 102 Active: true, 103 }, 104 { 105 PartitionID: 1, 106 Active: true, 107 }, 108 }, 109 RetentionPeriod: retentionPeriod, 110 RetentionStorageMB: 0, 111 SupportedCodecs: supportedCodecs, 112 PartitionWriteBurstBytes: burstBytes, 113 PartitionWriteSpeedBytesPerSecond: writeSpeed, 114 Attributes: nil, 115 Consumers: consumers, 116 MeteringMode: topictypes.MeteringModeUnspecified, 117 } 118 119 requireAndCleanSubset := func(checked *map[string]string, subset *map[string]string) { 120 t.Helper() 121 for k, subValue := range *subset { 122 checkedValue, ok := (*checked)[k] 123 require.True(t, ok, k) 124 require.Equal(t, subValue, checkedValue) 125 } 126 *checked = nil 127 *subset = nil 128 } 129 130 requireAndCleanSubset(&res.Attributes, &expected.Attributes) 131 132 for i := range expected.Consumers { 133 requireAndCleanSubset(&res.Consumers[i].Attributes, &expected.Consumers[i].Attributes) 134 } 135 136 require.Equal(t, expected, res) 137 } 138 139 func TestSchemeList(t *testing.T) { 140 ctx := xtest.Context(t) 141 db := connect(t) 142 143 topicPath := createTopic(ctx, t, db) 144 list, err := db.Scheme().ListDirectory(ctx, db.Name()) 145 require.NoError(t, err) 146 147 topicName := path.Base(topicPath) 148 149 hasTopic := false 150 for _, e := range list.Children { 151 if e.IsTopic() && topicName == e.Name { 152 hasTopic = true 153 } 154 } 155 require.True(t, hasTopic) 156 } 157 158 func connect(t testing.TB, opts ...ydb.Option) *ydb.Driver { 159 return connectWithLogOption(t, false, opts...) 160 } 161 162 func connectWithGrpcLogging(t testing.TB, opts ...ydb.Option) *ydb.Driver { 163 return connectWithLogOption(t, true, opts...) 164 } 165 166 func connectWithLogOption(t testing.TB, logGRPC bool, opts ...ydb.Option) *ydb.Driver { 167 connectionString := defaultConnectionString 168 if cs := os.Getenv("YDB_CONNECTION_STRING"); cs != "" { 169 connectionString = cs 170 } 171 172 var grpcOptions []grpc.DialOption 173 const needLogGRPCMessages = true 174 if logGRPC { 175 grpcOptions = append(grpcOptions, 176 grpc.WithChainUnaryInterceptor(xtest.NewGrpcLogger(t).UnaryClientInterceptor), 177 grpc.WithChainStreamInterceptor(xtest.NewGrpcLogger(t).StreamClientInterceptor), 178 ) 179 } 180 181 ydbOpts := []ydb.Option{ 182 ydb.WithDialTimeout(time.Second), 183 ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")), 184 ydb.With(config.WithGrpcOptions(grpcOptions...)), 185 } 186 ydbOpts = append(ydbOpts, opts...) 187 188 db, err := ydb.Open(context.Background(), connectionString, ydbOpts...) 189 require.NoError(t, err) 190 191 t.Cleanup(func() { 192 _ = db.Close(context.Background()) 193 }) 194 return db 195 }