|
| 1 | +//go:build !fast |
| 2 | +// +build !fast |
| 3 | + |
| 4 | +package test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto/tls" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "google.golang.org/grpc" |
| 14 | + "google.golang.org/grpc/metadata" |
| 15 | + |
| 16 | + ydb "github.com/ydb-platform/ydb-go-sdk/v3" |
| 17 | + "github.com/ydb-platform/ydb-go-sdk/v3/balancers" |
| 18 | + "github.com/ydb-platform/ydb-go-sdk/v3/config" |
| 19 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta" |
| 20 | + "github.com/ydb-platform/ydb-go-sdk/v3/log" |
| 21 | + "github.com/ydb-platform/ydb-go-sdk/v3/trace" |
| 22 | +) |
| 23 | + |
| 24 | +func TestDiscovery(t *testing.T) { |
| 25 | + var ( |
| 26 | + userAgent = "connection user agent" |
| 27 | + requestType = "connection request type" |
| 28 | + checkMedatada = func(ctx context.Context) { |
| 29 | + md, has := metadata.FromOutgoingContext(ctx) |
| 30 | + if !has { |
| 31 | + t.Fatalf("no medatada") |
| 32 | + } |
| 33 | + userAgents := md.Get(meta.HeaderUserAgent) |
| 34 | + if len(userAgents) == 0 { |
| 35 | + t.Fatalf("no user agent") |
| 36 | + } |
| 37 | + if userAgents[0] != userAgent { |
| 38 | + t.Fatalf("unknown user agent: %s", userAgents[0]) |
| 39 | + } |
| 40 | + requestTypes := md.Get(meta.HeaderRequestType) |
| 41 | + if len(requestTypes) == 0 { |
| 42 | + t.Fatalf("no request type") |
| 43 | + } |
| 44 | + if requestTypes[0] != requestType { |
| 45 | + t.Fatalf("unknown request type: %s", requestTypes[0]) |
| 46 | + } |
| 47 | + } |
| 48 | + ) |
| 49 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 50 | + defer cancel() |
| 51 | + db, err := ydb.New( |
| 52 | + ctx, |
| 53 | + ydb.WithConnectionString(os.Getenv("YDB_CONNECTION_STRING")), |
| 54 | + ydb.WithAnonymousCredentials(), |
| 55 | + ydb.With( |
| 56 | + config.WithOperationTimeout(time.Second*2), |
| 57 | + config.WithOperationCancelAfter(time.Second*2), |
| 58 | + ), |
| 59 | + ydb.WithBalancer(balancers.SingleConn()), |
| 60 | + ydb.WithConnectionTTL(time.Millisecond*500), |
| 61 | + ydb.WithMinTLSVersion(tls.VersionTLS10), |
| 62 | + ydb.WithLogger( |
| 63 | + trace.MatchDetails(`ydb\.(driver|discovery).*`), |
| 64 | + ydb.WithNamespace("ydb"), |
| 65 | + ydb.WithOutWriter(os.Stdout), |
| 66 | + ydb.WithErrWriter(os.Stderr), |
| 67 | + ydb.WithMinLevel(log.WARN), |
| 68 | + ), |
| 69 | + ydb.WithUserAgent(userAgent), |
| 70 | + ydb.WithRequestsType(requestType), |
| 71 | + ydb.With( |
| 72 | + config.WithGrpcOptions( |
| 73 | + grpc.WithUnaryInterceptor(func( |
| 74 | + ctx context.Context, |
| 75 | + method string, |
| 76 | + req, reply interface{}, |
| 77 | + cc *grpc.ClientConn, |
| 78 | + invoker grpc.UnaryInvoker, |
| 79 | + opts ...grpc.CallOption, |
| 80 | + ) error { |
| 81 | + checkMedatada(ctx) |
| 82 | + return invoker(ctx, method, req, reply, cc, opts...) |
| 83 | + }), |
| 84 | + grpc.WithStreamInterceptor(func( |
| 85 | + ctx context.Context, |
| 86 | + desc *grpc.StreamDesc, |
| 87 | + cc *grpc.ClientConn, |
| 88 | + method string, |
| 89 | + streamer grpc.Streamer, |
| 90 | + opts ...grpc.CallOption, |
| 91 | + ) (grpc.ClientStream, error) { |
| 92 | + checkMedatada(ctx) |
| 93 | + return streamer(ctx, desc, cc, method, opts...) |
| 94 | + }), |
| 95 | + ), |
| 96 | + ), |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + defer func() { |
| 102 | + // cleanup connection |
| 103 | + if e := db.Close(ctx); e != nil { |
| 104 | + t.Fatalf("db close failed: %+v", e) |
| 105 | + } |
| 106 | + }() |
| 107 | + t.Run("Discover", func(t *testing.T) { |
| 108 | + time.Sleep(time.Second) // wait for parking conn |
| 109 | + if _, err = db.Discovery().Discover(ctx); err != nil { |
| 110 | + t.Fatalf("Execute failed: %v", err) |
| 111 | + } |
| 112 | + }) |
| 113 | +} |
0 commit comments