| 104 | } |
| 105 | |
| 106 | func TestReloader_Run(t *testing.T) { |
| 107 | ctx, cancel := context.WithCancel(context.Background()) |
| 108 | defer cancel() |
| 109 | |
| 110 | var ncalls atomic.Int64 |
| 111 | load, err := New[string](ctx, ReloadOpts[string]{ |
| 112 | Logf: tstest.WhileTestRunningLogger(t), |
| 113 | Interval: 10 * time.Millisecond, |
| 114 | Read: func(context.Context) ([]byte, error) { |
| 115 | return []byte("hello"), nil |
| 116 | }, |
| 117 | Unmarshal: func(b []byte) (string, error) { |
| 118 | callNum := ncalls.Add(1) |
| 119 | if callNum == 3 { |
| 120 | cancel() |
| 121 | } |
| 122 | return fmt.Sprintf("call %d: %s", callNum, b), nil |
| 123 | }, |
| 124 | }) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | want := "call 1: hello" |
| 129 | if got := load(); got != want { |
| 130 | t.Fatalf("got value %q, want %q", got, want) |
| 131 | } |
| 132 | |
| 133 | // Wait for the periodic refresh to cancel our context |
| 134 | select { |
| 135 | case <-ctx.Done(): |
| 136 | case <-time.After(10 * time.Second): |
| 137 | t.Fatal("test timed out") |
| 138 | } |
| 139 | |
| 140 | // Depending on how goroutines get scheduled, we can either read call 2 |
| 141 | // (if we woke up before the run goroutine stores call 3), or call 3 |
| 142 | // (if we woke up after the run goroutine stores the next value). Check |
| 143 | // for both. |
| 144 | want1, want2 := "call 2: hello", "call 3: hello" |
| 145 | if got := load(); got != want1 && got != want2 { |
| 146 | t.Fatalf("got value %q, want %q or %q", got, want1, want2) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestFromJSONFile(t *testing.T) { |
| 151 | type testStruct struct { |