|
| 1 | +package testclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "cloud.google.com/go/pubsub" |
| 11 | + "cloud.google.com/go/pubsub/pstest" |
| 12 | + "github.com/GoogleCloudPlatform/opentelemetry-operations-e2e-testing/e2etesting/setuptf" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "google.golang.org/api/option" |
| 16 | + "google.golang.org/genproto/googleapis/rpc/code" |
| 17 | + "google.golang.org/grpc" |
| 18 | + "google.golang.org/grpc/credentials/insecure" |
| 19 | +) |
| 20 | + |
| 21 | +func TestClientRequest(t *testing.T) { |
| 22 | + ctx := context.Background() |
| 23 | + |
| 24 | + srv := pstest.NewServer() |
| 25 | + defer srv.Close() |
| 26 | + |
| 27 | + os.Setenv("PUBSUB_EMULATOR_HOST", srv.Addr) |
| 28 | + defer os.Unsetenv("PUBSUB_EMULATOR_HOST") |
| 29 | + |
| 30 | + conn, err := grpc.Dial(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 31 | + require.NoError(t, err) |
| 32 | + defer conn.Close() |
| 33 | + |
| 34 | + client, err := pubsub.NewClient(ctx, "project", option.WithGRPCConn(conn)) |
| 35 | + require.NoError(t, err) |
| 36 | + defer client.Close() |
| 37 | + |
| 38 | + pubsubInfo := &setuptf.PubsubInfo{ |
| 39 | + RequestTopic: setuptf.TopicInfo{ |
| 40 | + TopicName: "request-topic", |
| 41 | + }, |
| 42 | + ResponseTopic: setuptf.TopicInfo{ |
| 43 | + TopicName: "response-topic", |
| 44 | + SubscriptionName: "response-sub", |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + reqTopic, err := client.CreateTopic(ctx, "request-topic") |
| 49 | + require.NoError(t, err) |
| 50 | + reqSub, err := client.CreateSubscription(ctx, "request-sub", pubsub.SubscriptionConfig{ |
| 51 | + Topic: reqTopic, |
| 52 | + }) |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + respTopic, err := client.CreateTopic(ctx, "response-topic") |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + _, err = client.CreateSubscription(ctx, "response-sub", pubsub.SubscriptionConfig{ |
| 59 | + Topic: respTopic, |
| 60 | + }) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + // Mock server answering requests |
| 64 | + go func() { |
| 65 | + err := reqSub.Receive(ctx, func(c context.Context, msg *pubsub.Message) { |
| 66 | + msg.Ack() |
| 67 | + |
| 68 | + custom := "header" |
| 69 | + if val, ok := msg.Attributes["foo"]; ok { |
| 70 | + custom = val |
| 71 | + } |
| 72 | + |
| 73 | + // Send response |
| 74 | + res := &pubsub.Message{ |
| 75 | + Attributes: map[string]string{ |
| 76 | + TestID: msg.Attributes[TestID], |
| 77 | + StatusCode: strconv.Itoa(int(code.Code_OK)), |
| 78 | + "custom": custom, |
| 79 | + }, |
| 80 | + } |
| 81 | + respTopic.Publish(c, res) |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + t.Logf("reqSub receive error: %v", err) |
| 85 | + } |
| 86 | + }() |
| 87 | + |
| 88 | + sut, err := New(ctx, "project", pubsubInfo) |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + t.Run("single request", func(t *testing.T) { |
| 92 | + req := Request{ |
| 93 | + TestID: "test-123", |
| 94 | + Scenario: "my-scenario", |
| 95 | + Headers: map[string]string{"foo": "bar"}, |
| 96 | + } |
| 97 | + |
| 98 | + resp, err := sut.Request(ctx, req) |
| 99 | + require.NoError(t, err) |
| 100 | + require.NotNil(t, resp) |
| 101 | + assert.Equal(t, code.Code_OK, resp.StatusCode) |
| 102 | + assert.Equal(t, "bar", resp.Headers["custom"]) |
| 103 | + assert.Equal(t, "test-123", resp.Headers[TestID]) |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("multiplexing", func(t *testing.T) { |
| 107 | + var wg sync.WaitGroup |
| 108 | + for i := 0; i < 10; i++ { |
| 109 | + wg.Add(1) |
| 110 | + go func(idx int) { |
| 111 | + defer wg.Done() |
| 112 | + req := Request{ |
| 113 | + TestID: "test-" + strconv.Itoa(idx), |
| 114 | + Scenario: "my-scenario-" + strconv.Itoa(idx), |
| 115 | + Headers: map[string]string{"foo": "bar-" + strconv.Itoa(idx)}, |
| 116 | + } |
| 117 | + |
| 118 | + resp, err := sut.Request(ctx, req) |
| 119 | + assert.NoError(t, err) |
| 120 | + require.NotNil(t, resp) |
| 121 | + assert.Equal(t, code.Code_OK, resp.StatusCode) |
| 122 | + assert.Equal(t, "bar-"+strconv.Itoa(idx), resp.Headers["custom"]) |
| 123 | + assert.Equal(t, "test-"+strconv.Itoa(idx), resp.Headers[TestID]) |
| 124 | + }(i) |
| 125 | + } |
| 126 | + wg.Wait() |
| 127 | + }) |
| 128 | +} |
0 commit comments