|
| 1 | +package configwatcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/opendecree/decree/sdk/configclient" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewGroup_FillAllTypes(t *testing.T) { |
| 12 | + tr := &mockTransport{ |
| 13 | + getConfigFn: func(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 14 | + return &configclient.GetConfigResponse{ |
| 15 | + Values: []configclient.ConfigValue{ |
| 16 | + {FieldPath: "app.name", Value: configclient.StringVal("myapp")}, |
| 17 | + {FieldPath: "app.debug", Value: configclient.BoolVal(true)}, |
| 18 | + {FieldPath: "app.count", Value: configclient.IntVal(42)}, |
| 19 | + {FieldPath: "app.rate", Value: configclient.FloatVal(1.5)}, |
| 20 | + {FieldPath: "app.timeout", Value: configclient.DurationVal(5 * time.Second)}, |
| 21 | + }, |
| 22 | + }, nil |
| 23 | + }, |
| 24 | + subscribeFn: func(ctx context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 25 | + return newMockSubscription(ctx), nil |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + type Config struct { |
| 30 | + Name string `decree:"app.name"` |
| 31 | + Debug bool `decree:"app.debug"` |
| 32 | + Count int64 `decree:"app.count"` |
| 33 | + Rate float64 `decree:"app.rate"` |
| 34 | + Timeout time.Duration `decree:"app.timeout"` |
| 35 | + Ignored string |
| 36 | + Skipped string `decree:"-"` |
| 37 | + } |
| 38 | + |
| 39 | + w := New(tr, "t1") |
| 40 | + g, err := w.NewGroup(&Config{}) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("NewGroup: %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 | + var cfg Config |
| 50 | + if err := g.Fill(&cfg); err != nil { |
| 51 | + t.Fatalf("Fill: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + if cfg.Name != "myapp" { |
| 55 | + t.Errorf("Name = %q, want %q", cfg.Name, "myapp") |
| 56 | + } |
| 57 | + if !cfg.Debug { |
| 58 | + t.Error("Debug = false, want true") |
| 59 | + } |
| 60 | + if cfg.Count != 42 { |
| 61 | + t.Errorf("Count = %d, want 42", cfg.Count) |
| 62 | + } |
| 63 | + if cfg.Rate != 1.5 { |
| 64 | + t.Errorf("Rate = %f, want 1.5", cfg.Rate) |
| 65 | + } |
| 66 | + if cfg.Timeout != 5*time.Second { |
| 67 | + t.Errorf("Timeout = %v, want 5s", cfg.Timeout) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestNewGroup_NonPointerError(t *testing.T) { |
| 72 | + tr := &mockTransport{ |
| 73 | + getConfigFn: func(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 74 | + return &configclient.GetConfigResponse{}, nil |
| 75 | + }, |
| 76 | + subscribeFn: func(ctx context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 77 | + return newMockSubscription(ctx), nil |
| 78 | + }, |
| 79 | + } |
| 80 | + w := New(tr, "t1") |
| 81 | + type S struct{} |
| 82 | + _, err := w.NewGroup(S{}) |
| 83 | + if err == nil { |
| 84 | + t.Error("expected error for non-pointer, got nil") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestGroup_Fill_TypeMismatch(t *testing.T) { |
| 89 | + tr := &mockTransport{ |
| 90 | + getConfigFn: func(_ context.Context, _ *configclient.GetConfigRequest) (*configclient.GetConfigResponse, error) { |
| 91 | + return &configclient.GetConfigResponse{}, nil |
| 92 | + }, |
| 93 | + subscribeFn: func(ctx context.Context, _ *configclient.SubscribeRequest) (configclient.Subscription, error) { |
| 94 | + return newMockSubscription(ctx), nil |
| 95 | + }, |
| 96 | + } |
| 97 | + w := New(tr, "t1") |
| 98 | + type A struct { |
| 99 | + X string `decree:"x"` |
| 100 | + } |
| 101 | + type B struct { |
| 102 | + X string `decree:"x"` |
| 103 | + } |
| 104 | + |
| 105 | + g, err := w.NewGroup(&A{}) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("NewGroup: %v", err) |
| 108 | + } |
| 109 | + var b B |
| 110 | + if err := g.Fill(&b); err == nil { |
| 111 | + t.Error("expected type mismatch error, got nil") |
| 112 | + } |
| 113 | +} |
0 commit comments