|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "google.golang.org/grpc/credentials/insecure" |
| 11 | + "k8s.io/utils/ptr" |
| 12 | + |
| 13 | + "google.golang.org/grpc" |
| 14 | + "google.golang.org/grpc/test/bufconn" |
| 15 | + "google.golang.org/protobuf/types/known/emptypb" |
| 16 | + |
| 17 | + pbv1 "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options/grpc/protobuf/v1" |
| 18 | + "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types" |
| 19 | +) |
| 20 | + |
| 21 | +const bufSize = 1024 * 1024 |
| 22 | + |
| 23 | +// mockCloudEventService implements a basic mock of CloudEventServiceServer |
| 24 | +type mockCloudEventService struct { |
| 25 | + pbv1.UnimplementedCloudEventServiceServer |
| 26 | + healthEnabled bool |
| 27 | +} |
| 28 | + |
| 29 | +func (m *mockCloudEventService) Publish(ctx context.Context, req *pbv1.PublishRequest) (*emptypb.Empty, error) { |
| 30 | + return &emptypb.Empty{}, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (m *mockCloudEventService) Subscribe(req *pbv1.SubscriptionRequest, stream pbv1.CloudEventService_SubscribeServer) error { |
| 34 | + if m.healthEnabled { |
| 35 | + go func() { |
| 36 | + ticker := time.NewTicker(500 * time.Millisecond) |
| 37 | + defer ticker.Stop() |
| 38 | + |
| 39 | + for { |
| 40 | + select { |
| 41 | + case <-ticker.C: |
| 42 | + heartbeat := &pbv1.CloudEvent{ |
| 43 | + SpecVersion: "1.0", |
| 44 | + Id: uuid.New().String(), |
| 45 | + Type: types.HeartbeatCloudEventsType, |
| 46 | + } |
| 47 | + |
| 48 | + if err := stream.Send(heartbeat); err != nil { |
| 49 | + return |
| 50 | + } |
| 51 | + case <-stream.Context().Done(): |
| 52 | + return |
| 53 | + } |
| 54 | + } |
| 55 | + }() |
| 56 | + } |
| 57 | + // Keep the stream open for testing |
| 58 | + <-stream.Context().Done() |
| 59 | + return stream.Context().Err() |
| 60 | +} |
| 61 | + |
| 62 | +func setupMockServer(t *testing.T, healthEnabled bool) (*grpc.ClientConn, func()) { |
| 63 | + lis := bufconn.Listen(bufSize) |
| 64 | + s := grpc.NewServer() |
| 65 | + |
| 66 | + // Register cloud event service |
| 67 | + pbv1.RegisterCloudEventServiceServer(s, &mockCloudEventService{healthEnabled: healthEnabled}) |
| 68 | + |
| 69 | + go func() { |
| 70 | + if err := s.Serve(lis); err != nil { |
| 71 | + t.Logf("Server exited with error: %v", err) |
| 72 | + } |
| 73 | + }() |
| 74 | + |
| 75 | + // Wait for server to start accepting connections |
| 76 | + time.Sleep(50 * time.Millisecond) |
| 77 | + |
| 78 | + bufDialer := func(context.Context, string) (net.Conn, error) { |
| 79 | + return lis.Dial() |
| 80 | + } |
| 81 | + |
| 82 | + conn, err := grpc.NewClient("passthrough:///bufnet", |
| 83 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 84 | + grpc.WithContextDialer(bufDialer)) |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Failed to create client: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + cleanup := func() { |
| 91 | + conn.Close() |
| 92 | + s.Stop() |
| 93 | + } |
| 94 | + |
| 95 | + return conn, cleanup |
| 96 | +} |
| 97 | + |
| 98 | +func TestProtocol_Success(t *testing.T) { |
| 99 | + conn, cleanup := setupMockServer(t, true) |
| 100 | + defer cleanup() |
| 101 | + |
| 102 | + reconnectErrorChan := make(chan error, 1) |
| 103 | + p, err := NewProtocol( |
| 104 | + conn, |
| 105 | + WithSubscribeOption(&SubscribeOption{ |
| 106 | + Source: "test", |
| 107 | + ClusterName: "test-cluster", |
| 108 | + DataType: "io.open-cluster-management.test", |
| 109 | + }), |
| 110 | + WithReconnectErrorChan(reconnectErrorChan), |
| 111 | + WithServerHealthinessTimeout(ptr.To(5*time.Second)), |
| 112 | + ) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 118 | + defer cancel() |
| 119 | + |
| 120 | + go func() { |
| 121 | + if err := p.OpenInbound(ctx); err != nil { |
| 122 | + p.reconnectErrorChan <- err |
| 123 | + } |
| 124 | + }() |
| 125 | + |
| 126 | + // Should not receive any error since health check was successful |
| 127 | + select { |
| 128 | + case err := <-p.reconnectErrorChan: |
| 129 | + t.Errorf("Unexpected error from health check: %v", err) |
| 130 | + case <-time.After(2 * time.Second): |
| 131 | + // Expected - no error |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestProtocol_Timeout(t *testing.T) { |
| 136 | + conn, cleanup := setupMockServer(t, false) // No health service |
| 137 | + defer cleanup() |
| 138 | + |
| 139 | + reconnectErrorChan := make(chan error, 1) |
| 140 | + p, err := NewProtocol( |
| 141 | + conn, |
| 142 | + WithSubscribeOption(&SubscribeOption{ |
| 143 | + Source: "test", |
| 144 | + ClusterName: "test-cluster", |
| 145 | + DataType: "io.open-cluster-management.test", |
| 146 | + }), |
| 147 | + WithReconnectErrorChan(reconnectErrorChan), |
| 148 | + WithServerHealthinessTimeout(ptr.To(time.Second)), |
| 149 | + ) |
| 150 | + if err != nil { |
| 151 | + t.Fatal(err) |
| 152 | + } |
| 153 | + |
| 154 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 155 | + defer cancel() |
| 156 | + |
| 157 | + go func() { |
| 158 | + if err := p.OpenInbound(ctx); err != nil { |
| 159 | + p.reconnectErrorChan <- err |
| 160 | + } |
| 161 | + }() |
| 162 | + |
| 163 | + // Should receive an error due to health service not being available |
| 164 | + select { |
| 165 | + case err := <-p.reconnectErrorChan: |
| 166 | + if err == nil { |
| 167 | + t.Errorf("Expected health check error, but got nil %v", err) |
| 168 | + } |
| 169 | + case <-time.After(3 * time.Second): |
| 170 | + t.Error("Expected health check error within timeout") |
| 171 | + } |
| 172 | +} |
0 commit comments