Skip to content

Commit f38fca8

Browse files
authored
Merge pull request kubernetes#130024 from sivchari/propagate-context
Propagate cotnext to avoid goroutine leak
2 parents fbdf890 + 03b59d8 commit f38fca8

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

test/integration/kubelet/watch_manager_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ import (
4040
func TestWatchBasedManager(t *testing.T) {
4141
testNamespace := "test-watch-based-manager"
4242
server := kubeapiservertesting.StartTestServerOrDie(t, nil, framework.DefaultTestServerFlags(), framework.SharedEtcd())
43-
defer server.TearDownFn()
43+
t.Cleanup(server.TearDownFn)
44+
45+
ctx, cancel := context.WithCancel(context.Background())
46+
t.Cleanup(cancel)
4447

4548
const n = 10
4649
server.ClientConfig.QPS = 10000
@@ -49,15 +52,15 @@ func TestWatchBasedManager(t *testing.T) {
4952
if err != nil {
5053
t.Fatalf("unexpected error: %v", err)
5154
}
52-
if _, err := client.CoreV1().Namespaces().Create(context.TODO(), (&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}), metav1.CreateOptions{}); err != nil {
55+
if _, err := client.CoreV1().Namespaces().Create(ctx, (&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}), metav1.CreateOptions{}); err != nil {
5356
t.Fatal(err)
5457
}
5558

5659
listObj := func(namespace string, options metav1.ListOptions) (runtime.Object, error) {
57-
return client.CoreV1().Secrets(namespace).List(context.TODO(), options)
60+
return client.CoreV1().Secrets(namespace).List(ctx, options)
5861
}
5962
watchObj := func(namespace string, options metav1.ListOptions) (watch.Interface, error) {
60-
return client.CoreV1().Secrets(namespace).Watch(context.TODO(), options)
63+
return client.CoreV1().Secrets(namespace).Watch(ctx, options)
6164
}
6265
newObj := func() runtime.Object { return &v1.Secret{} }
6366
// We want all watches to be up and running to stress test it.
@@ -66,7 +69,8 @@ func TestWatchBasedManager(t *testing.T) {
6669
fakeClock := testingclock.NewFakeClock(time.Now())
6770

6871
stopCh := make(chan struct{})
69-
defer close(stopCh)
72+
t.Cleanup(func() { close(stopCh) })
73+
7074
store := manager.NewObjectCache(listObj, watchObj, newObj, isImmutable, schema.GroupResource{Group: "v1", Resource: "secrets"}, fakeClock, time.Minute, stopCh)
7175

7276
// create 1000 secrets in parallel
@@ -79,7 +83,7 @@ func TestWatchBasedManager(t *testing.T) {
7983
defer wg.Done()
8084
for j := 0; j < 100; j++ {
8185
name := fmt.Sprintf("s%d", i*100+j)
82-
if _, err := client.CoreV1().Secrets(testNamespace).Create(context.TODO(), &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: name}}, metav1.CreateOptions{}); err != nil {
86+
if _, err := client.CoreV1().Secrets(testNamespace).Create(ctx, &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: name}}, metav1.CreateOptions{}); err != nil {
8387
select {
8488
case errCh <- err:
8589
default:

0 commit comments

Comments
 (0)