@@ -28,6 +28,8 @@ import (
28
28
"testing"
29
29
"time"
30
30
31
+ "github.com/stretchr/testify/require"
32
+
31
33
v1 "k8s.io/api/core/v1"
32
34
apierrors "k8s.io/apimachinery/pkg/api/errors"
33
35
"k8s.io/apimachinery/pkg/api/meta"
@@ -124,6 +126,26 @@ func TestReflectorResyncChan(t *testing.T) {
124
126
}
125
127
}
126
128
129
+ // TestEstablishedWatchStoppedAfterStopCh ensures that
130
+ // an established watch will be closed right after
131
+ // the StopCh was also closed.
132
+ func TestEstablishedWatchStoppedAfterStopCh (t * testing.T ) {
133
+ ctx , ctxCancel := context .WithCancel (context .TODO ())
134
+ ctxCancel ()
135
+ w := watch .NewFake ()
136
+ require .False (t , w .IsStopped ())
137
+
138
+ // w is stopped when the stopCh is closed
139
+ target := NewReflector (nil , & v1.Pod {}, nil , 0 )
140
+ err := target .watch (w , ctx .Done (), nil )
141
+ require .NoError (t , err )
142
+ require .True (t , w .IsStopped ())
143
+
144
+ // noop when the w is nil and the ctx is closed
145
+ err = target .watch (nil , ctx .Done (), nil )
146
+ require .NoError (t , err )
147
+ }
148
+
127
149
func BenchmarkReflectorResyncChanMany (b * testing.B ) {
128
150
s := NewStore (MetaNamespaceKeyFunc )
129
151
g := NewReflector (& testLW {}, & v1.Pod {}, s , 25 * time .Millisecond )
0 commit comments