| 19 | ) |
| 20 | |
| 21 | func TestChecker_Notify(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | |
| 24 | responses := []github.RepositoryRelease{ |
| 25 | {TagName: github.String("v1.2.3"), HTMLURL: github.String("https://someurl.com")}, |
| 26 | {TagName: github.String("v1.2.4"), HTMLURL: github.String("https://someurl.com")}, |
| 27 | {TagName: github.String("v1.2.4"), HTMLURL: github.String("https://someurl.com")}, |
| 28 | {TagName: github.String("v1.2.5"), HTMLURL: github.String("https://someurl.com")}, |
| 29 | } |
| 30 | responseC := make(chan github.RepositoryRelease, len(responses)) |
| 31 | for _, r := range responses { |
| 32 | responseC <- r |
| 33 | } |
| 34 | |
| 35 | wantVersion := []string{"v1.2.3", "v1.2.4", "v1.2.5"} |
| 36 | |
| 37 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 38 | select { |
| 39 | case <-r.Context().Done(): |
| 40 | case resp := <-responseC: |
| 41 | b, err := json.Marshal(resp) |
| 42 | assert.NoError(t, err) |
| 43 | |
| 44 | w.Header().Set("Content-Type", "application/json") |
| 45 | w.WriteHeader(http.StatusOK) |
| 46 | _, _ = w.Write(b) |
| 47 | } |
| 48 | })) |
| 49 | defer srv.Close() |
| 50 | |
| 51 | db, _ := dbtestutil.NewDB(t) |
| 52 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Named(t.Name()) |
| 53 | notify := make(chan updatecheck.Result, len(wantVersion)) |
| 54 | c := updatecheck.New(db, logger, updatecheck.Options{ |
| 55 | Interval: 1 * time.Nanosecond, // Zero means unset. |
| 56 | URL: srv.URL, |
| 57 | Notify: func(r updatecheck.Result) { |
| 58 | select { |
| 59 | case notify <- r: |
| 60 | default: |
| 61 | t.Error("unexpected notification") |
| 62 | } |
| 63 | }, |
| 64 | }) |
| 65 | defer c.Close() |
| 66 | |
| 67 | ctx := testutil.Context(t, testutil.WaitLong) |
| 68 | |
| 69 | for i := 0; i < len(wantVersion); i++ { |
| 70 | select { |
| 71 | case <-ctx.Done(): |
| 72 | t.Error("timed out waiting for notification") |
| 73 | case r := <-notify: |
| 74 | assert.Equal(t, wantVersion[i], r.Version) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |