@@ -73,7 +73,7 @@ func TestObserve_OnInit(t *testing.T) {
7373 defer cancel ()
7474
7575 client := fakeClient ()
76- client .Opener [* path ] = fake.Object {
76+ client .Opener . Paths [* path ] = fake.Object {
7777 Data : string (mustMarshalConfig (test .config )),
7878 Attrs : & storage.ReaderObjectAttrs {
7979 Generation : test .configGeneration ,
@@ -108,6 +108,87 @@ func TestObserve_OnInit(t *testing.T) {
108108 }
109109}
110110
111+ func TestObserve_OnInitRetry (t * testing.T ) {
112+ tests := []struct {
113+ name string
114+ config * configpb.Configuration
115+ configGeneration int64
116+ openErr error
117+ openOnRetry bool
118+ expectInitial * configpb.Dashboard
119+ expectError bool
120+ }{
121+ {
122+ name : "Reads config on retry" ,
123+ config : & configpb.Configuration {
124+ Dashboards : []* configpb.Dashboard {
125+ {
126+ Name : "dashboard" ,
127+ },
128+ },
129+ },
130+ openErr : errors .New ("fake error" ),
131+ openOnRetry : true ,
132+ expectInitial : & configpb.Dashboard {
133+ Name : "dashboard" ,
134+ },
135+ },
136+ {
137+ name : "Returns error if config isn't present on retry" ,
138+ openErr : errors .New ("fake error" ),
139+ expectError : true ,
140+ },
141+ }
142+
143+ path , err := gcs .NewPath ("gs://config/example" )
144+ if err != nil {
145+ t .Fatal ("could not path" )
146+ }
147+
148+ for _ , test := range tests {
149+ t .Run (test .name , func (t * testing.T ) {
150+ ctx , cancel := context .WithCancel (context .Background ())
151+ defer cancel ()
152+
153+ client := fakeClient ()
154+ client .Opener .Paths [* path ] = fake.Object {
155+ Data : string (mustMarshalConfig (test .config )),
156+ Attrs : & storage.ReaderObjectAttrs {
157+ Generation : 1 ,
158+ },
159+ OpenErr : test .openErr ,
160+ OpenOnRetry : test .openOnRetry ,
161+ }
162+ client .Stater [* path ] = fake.Stat {
163+ Attrs : storage.ObjectAttrs {
164+ Generation : 1 ,
165+ },
166+ }
167+
168+ snaps , err := Observe (ctx , nil , client , * path , nil )
169+
170+ if ! test .expectError && err != nil {
171+ t .Errorf ("Observe() got unexpected error: %v" , err )
172+ } else if test .expectError && err == nil {
173+ t .Errorf ("Observe() did not error as expected." )
174+ }
175+
176+ if test .expectInitial == nil {
177+ return
178+ }
179+
180+ select {
181+ case cs := <- snaps :
182+ if result := cs .Dashboards ["dashboard" ]; ! proto .Equal (result , test .expectInitial ) {
183+ t .Errorf ("got dashboard %v, expected %v" , result , test .expectInitial )
184+ }
185+ case <- time .After (30 * time .Second ):
186+ t .Error ("expected an initial snapshot, but got none" )
187+ }
188+ })
189+ }
190+ }
191+
111192func TestObserve_OnTick (t * testing.T ) {
112193 tests := []struct {
113194 name string
@@ -169,7 +250,7 @@ func TestObserve_OnTick(t *testing.T) {
169250 defer cancel ()
170251
171252 client := fakeClient ()
172- client .Opener [* path ] = fake.Object {
253+ client .Opener . Paths [* path ] = fake.Object {
173254 Data : string (mustMarshalConfig (initialConfig )),
174255 Attrs : & storage.ReaderObjectAttrs {
175256 Generation : 1 ,
@@ -190,7 +271,7 @@ func TestObserve_OnTick(t *testing.T) {
190271 <- snaps
191272
192273 // Change the config
193- client .Opener [* path ] = fake.Object {
274+ client .Opener . Paths [* path ] = fake.Object {
194275 Data : string (mustMarshalConfig (test .config )),
195276 Attrs : & storage.ReaderObjectAttrs {
196277 Generation : test .configGeneration ,
@@ -350,7 +431,7 @@ func TestObserve_Data(t *testing.T) {
350431 defer cancel ()
351432
352433 client := fakeClient ()
353- client .Opener [* path ] = fake.Object {
434+ client .Opener . Paths [* path ] = fake.Object {
354435 Data : string (mustMarshalConfig (test .config )),
355436 Attrs : & storage.ReaderObjectAttrs {
356437 Generation : 1 ,
@@ -394,7 +475,10 @@ func fakeClient() *fake.ConditionalClient {
394475 Uploader : fake.Uploader {},
395476 Client : fake.Client {
396477 Lister : fake.Lister {},
397- Opener : fake.Opener {},
478+ Opener : fake.Opener {
479+ Paths : map [gcs.Path ]fake.Object {},
480+ Lock : & sync.RWMutex {},
481+ },
398482 },
399483 Stater : fake.Stater {},
400484 },
0 commit comments