|
| 1 | +package configwatcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/opendecree/decree/sdk/configclient" |
| 9 | +) |
| 10 | + |
| 11 | +type writeTransport struct { |
| 12 | + mockTransport |
| 13 | + setFieldFn func(ctx context.Context, req *configclient.SetFieldRequest) (*configclient.SetFieldResponse, error) |
| 14 | +} |
| 15 | + |
| 16 | +func (t *writeTransport) SetField(ctx context.Context, req *configclient.SetFieldRequest) (*configclient.SetFieldResponse, error) { |
| 17 | + return t.setFieldFn(ctx, req) |
| 18 | +} |
| 19 | + |
| 20 | +func TestSetString_WritesAndUpdatesLocal(t *testing.T) { |
| 21 | + var written *configclient.SetFieldRequest |
| 22 | + tr := &writeTransport{ |
| 23 | + mockTransport: mockTransport{ |
| 24 | + getConfigFn: func(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 25 | + return &configclient.GetConfigResponse{ |
| 26 | + Values: []configclient.ConfigValue{{FieldPath: "app.env", Value: configclient.StringVal("staging")}}, |
| 27 | + }, nil |
| 28 | + }, |
| 29 | + subscribeFn: func(ctx context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 30 | + return newMockSubscription(ctx), nil |
| 31 | + }, |
| 32 | + }, |
| 33 | + setFieldFn: func(_ context.Context, req *configclient.SetFieldRequest) (*configclient.SetFieldResponse, error) { |
| 34 | + written = req |
| 35 | + return &configclient.SetFieldResponse{}, nil |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + w := New(tr, "tenant1") |
| 40 | + val, err := w.String("app.env", "default") |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("String: %v", err) |
| 43 | + } |
| 44 | + if err := w.Start(context.Background()); err != nil { |
| 45 | + t.Fatalf("Start: %v", err) |
| 46 | + } |
| 47 | + defer w.Close() |
| 48 | + |
| 49 | + if err := w.SetString(context.Background(), "app.env", "production"); err != nil { |
| 50 | + t.Fatalf("SetString: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Server was called with correct params. |
| 54 | + if written == nil || written.FieldPath != "app.env" { |
| 55 | + t.Errorf("SetField not called with expected field path, got %+v", written) |
| 56 | + } |
| 57 | + sv, _ := written.Value.StringValue() |
| 58 | + if sv != "production" { |
| 59 | + t.Errorf("SetField value = %q, want %q", sv, "production") |
| 60 | + } |
| 61 | + |
| 62 | + // Optimistic local update applied. |
| 63 | + if got := val.Get(); got != "production" { |
| 64 | + t.Errorf("local value = %q, want %q", got, "production") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestSetString_TransportError(t *testing.T) { |
| 69 | + tr := &writeTransport{ |
| 70 | + mockTransport: mockTransport{ |
| 71 | + getConfigFn: func(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 72 | + return &configclient.GetConfigResponse{}, nil |
| 73 | + }, |
| 74 | + subscribeFn: func(ctx context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 75 | + return newMockSubscription(ctx), nil |
| 76 | + }, |
| 77 | + }, |
| 78 | + setFieldFn: func(_ context.Context, _ *configclient.SetFieldRequest) (*configclient.SetFieldResponse, error) { |
| 79 | + return nil, errors.New("server error") |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + w := New(tr, "tenant1") |
| 84 | + if err := w.Start(context.Background()); err != nil { |
| 85 | + t.Fatalf("Start: %v", err) |
| 86 | + } |
| 87 | + defer w.Close() |
| 88 | + |
| 89 | + err := w.SetString(context.Background(), "app.env", "production") |
| 90 | + if err == nil { |
| 91 | + t.Error("expected error from transport, got nil") |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestSetBool_WriteThroughOptions(t *testing.T) { |
| 96 | + var req *configclient.SetFieldRequest |
| 97 | + tr := &writeTransport{ |
| 98 | + mockTransport: mockTransport{ |
| 99 | + getConfigFn: func(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 100 | + return &configclient.GetConfigResponse{}, nil |
| 101 | + }, |
| 102 | + subscribeFn: func(ctx context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 103 | + return newMockSubscription(ctx), nil |
| 104 | + }, |
| 105 | + }, |
| 106 | + setFieldFn: func(_ context.Context, r *configclient.SetFieldRequest) (*configclient.SetFieldResponse, error) { |
| 107 | + req = r |
| 108 | + return &configclient.SetFieldResponse{}, nil |
| 109 | + }, |
| 110 | + } |
| 111 | + w := New(tr, "tenant1") |
| 112 | + if err := w.Start(context.Background()); err != nil { |
| 113 | + t.Fatalf("Start: %v", err) |
| 114 | + } |
| 115 | + defer w.Close() |
| 116 | + |
| 117 | + if err := w.SetBool(context.Background(), "app.debug", true, |
| 118 | + WithDescription("enable debug"), |
| 119 | + WithExpectedChecksum("abc123"), |
| 120 | + ); err != nil { |
| 121 | + t.Fatalf("SetBool: %v", err) |
| 122 | + } |
| 123 | + if req.Description != "enable debug" { |
| 124 | + t.Errorf("Description = %q, want %q", req.Description, "enable debug") |
| 125 | + } |
| 126 | + if req.ExpectedChecksum == nil || *req.ExpectedChecksum != "abc123" { |
| 127 | + t.Errorf("ExpectedChecksum = %v, want abc123", req.ExpectedChecksum) |
| 128 | + } |
| 129 | +} |
0 commit comments