Skip to content

Commit a5b111a

Browse files
committed
Fix tests
1 parent 7bb4c33 commit a5b111a

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

handlers/upload_droplet/upload_droplet_test.go

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package upload_droplet_test
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"fmt"
78
"net/http"
@@ -11,7 +12,6 @@ import (
1112
"time"
1213

1314
"code.cloudfoundry.org/cc-uploader/ccclient/fake_ccclient"
14-
"code.cloudfoundry.org/cc-uploader/handlers/test_helpers"
1515
"code.cloudfoundry.org/cc-uploader/handlers/upload_droplet"
1616
"code.cloudfoundry.org/lager/v3"
1717
"code.cloudfoundry.org/runtimeschema/cc_messages"
@@ -178,55 +178,67 @@ var _ = Describe("UploadDroplet", func() {
178178
})
179179

180180
Context("when the requester (client) goes away", func() {
181-
var fakeResponseWriter *test_helpers.FakeResponseWriter
182181

183-
BeforeEach(func() {
184-
var err error
185-
incomingRequest, err = http.NewRequest(
182+
It("responds with an error code when the client goes away during upload", func() {
183+
ctx, cancel := context.WithCancel(context.Background())
184+
req, err := http.NewRequestWithContext(ctx,
186185
"POST",
187186
fmt.Sprintf("http://example.com?%s=upload-uri.com", cc_messages.CcDropletUploadUriKey),
188187
bytes.NewBufferString(""),
189188
)
190189
Expect(err).NotTo(HaveOccurred())
191-
})
192190

193-
Context("and we are uploading", func() {
194-
BeforeEach(func() {
195-
closedChan := make(chan bool)
196-
fakeResponseWriter = test_helpers.NewFakeResponseWriter(closedChan)
197-
responseWriter = fakeResponseWriter
191+
rec := httptest.NewRecorder()
198192

199-
uploader.UploadStub = func(uploadURL *url.URL, filename string, r *http.Request, cancelChan <-chan struct{}) (*http.Response, error) {
200-
closedChan <- true
201-
Eventually(cancelChan).Should(BeClosed())
202-
return nil, errors.New("cancelled")
203-
}
204-
})
193+
uploader.UploadStub = func(_ *url.URL, _ string, _ *http.Request, cancelChan <-chan struct{}) (*http.Response, error) {
194+
<-cancelChan
195+
return nil, errors.New("cancelled")
196+
}
205197

206-
It("responds with an error code", func() {
207-
Expect(fakeResponseWriter.Code).To(Equal(http.StatusInternalServerError))
208-
})
198+
done := make(chan struct{})
199+
go func() {
200+
h := upload_droplet.New(&uploader, &poller, lager.NewLogger("fake-logger"), &sync.WaitGroup{})
201+
h.ServeHTTP(rec, req)
202+
close(done)
203+
}()
204+
205+
time.Sleep(100 * time.Millisecond)
206+
cancel()
207+
208+
Eventually(done, 2*time.Second).Should(BeClosed())
209+
Expect(rec.Code).To(Equal(http.StatusInternalServerError))
209210
})
210211

211-
Context("and we are polling", func() {
212-
BeforeEach(func() {
213-
uploadResponse := &http.Response{StatusCode: http.StatusOK}
214-
uploader.UploadReturns(uploadResponse, nil)
212+
It("responds with an error code when the client goes away during polling", func() {
213+
ctx, cancel := context.WithCancel(context.Background())
214+
req, err := http.NewRequestWithContext(ctx,
215+
"POST",
216+
fmt.Sprintf("http://example.com?%s=upload-uri.com", cc_messages.CcDropletUploadUriKey),
217+
bytes.NewBufferString(""),
218+
)
219+
Expect(err).NotTo(HaveOccurred())
215220

216-
closedChan := make(chan bool)
217-
fakeResponseWriter = test_helpers.NewFakeResponseWriter(closedChan)
218-
responseWriter = fakeResponseWriter
221+
rec := httptest.NewRecorder()
219222

220-
poller.PollStub = func(fallbackURL *url.URL, res *http.Response, cancelChan <-chan struct{}) error {
221-
closedChan <- true
222-
Eventually(cancelChan).Should(BeClosed())
223-
return errors.New("cancelled")
224-
}
225-
})
223+
uploader.UploadReturns(&http.Response{StatusCode: http.StatusOK}, nil)
226224

227-
It("responds with an error code", func() {
228-
Expect(fakeResponseWriter.Code).To(Equal(http.StatusInternalServerError))
229-
})
225+
poller.PollStub = func(_ *url.URL, _ *http.Response, cancelChan <-chan struct{}) error {
226+
<-cancelChan
227+
return errors.New("cancelled")
228+
}
229+
230+
done := make(chan struct{})
231+
go func() {
232+
h := upload_droplet.New(&uploader, &poller, lager.NewLogger("fake-logger"), &sync.WaitGroup{})
233+
h.ServeHTTP(rec, req)
234+
close(done)
235+
}()
236+
237+
time.Sleep(100 * time.Millisecond)
238+
cancel()
239+
240+
Eventually(done, 2*time.Second).Should(BeClosed())
241+
Expect(rec.Code).To(Equal(http.StatusInternalServerError))
230242
})
231243
})
232244

0 commit comments

Comments
 (0)