watcher: Clean up test
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rob Watson 2022-06-22 20:57:06 +02:00
parent 7f5fe3a717
commit 680f5b65e7
1 changed files with 8 additions and 11 deletions

View File

@ -109,11 +109,6 @@ func TestWatcherClosedChannel(t *testing.T) {
} }
func TestWatcherWithPodWatcher(t *testing.T) { func TestWatcherWithPodWatcher(t *testing.T) {
// TODO: use watcher.Close() to clean up watcher instead of relying on
// context, which leads to occasional race conditions in tests.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
deploymentsWatcher := watch.NewFake() deploymentsWatcher := watch.NewFake()
defer deploymentsWatcher.Stop() defer deploymentsWatcher.Stop()
@ -126,6 +121,10 @@ func TestWatcherWithPodWatcher(t *testing.T) {
untypedClient.PrependWatchReactor("deployments", k8stest.DefaultWatchReactor(deploymentsWatcher, nil)) untypedClient.PrependWatchReactor("deployments", k8stest.DefaultWatchReactor(deploymentsWatcher, nil))
client := logs.KubernetesClient{Typed: typedClient, Untyped: untypedClient} client := logs.KubernetesClient{Typed: typedClient, Untyped: untypedClient}
var buf bytes.Buffer
params := logs.WatcherParams{Name: "mydeployment", Type: "deployments", Namespace: "default"}
watcher := logs.NewWatcher(params, client, logs.NewPodWatcher, &buf, discardLogger())
go func() { go func() {
deployment := buildDeployment(t, "mydeployment") deployment := buildDeployment(t, "mydeployment")
deploymentsWatcher.Add(deployment) deploymentsWatcher.Add(deployment)
@ -140,14 +139,12 @@ func TestWatcherWithPodWatcher(t *testing.T) {
podsWatcher.Add(pod) podsWatcher.Add(pod)
time.Sleep(time.Millisecond * 250) time.Sleep(time.Millisecond * 250)
} }
watcher.Close()
}() }()
var buf bytes.Buffer err := watcher.Watch(context.Background())
params := logs.WatcherParams{Name: "mydeployment", Type: "deployments", Namespace: "default"} require.NoError(t, err)
watcher := logs.NewWatcher(params, client, logs.NewPodWatcher, &buf, discardLogger())
err := watcher.Watch(ctx)
require.EqualError(t, err, context.DeadlineExceeded.Error())
lines := bufToLines(&buf) lines := bufToLines(&buf)
require.Len(t, lines, 2) require.Len(t, lines, 2)
assert.ElementsMatch(t, []string{"[foo] fake logs", "[bar] fake logs"}, lines) assert.ElementsMatch(t, []string{"[foo] fake logs", "[bar] fake logs"}, lines)