TUN-6725: Fix testProxySSEAllData

This test was failing on Windows. We did not catch it before because our
TeamCity Windows builds were ignoring failed unit tests: TUN-6727

 - the fix is implementing WriteString for mockSSERespWriter
 - reason is because cfio.Copy was calling that, and not Write method,
   thus not triggering the usage of the channel for the test to continue
 - mockSSERespWriter was providing a valid implementation of WriteString
   via ResponseRecorder, which it implements via the embedded mockHTTPRespWriter
 - it is not clear why this only happened on Windows
 - changed it to be a top-level test since it did not share any code
   with other sub-tests in the same top-level test
This commit is contained in:
Nuno Diegues 2022-09-01 15:41:33 +01:00
parent 075ac1acf1
commit 4ac68711cd
1 changed files with 11 additions and 10 deletions

View File

@ -130,6 +130,10 @@ func (w *mockSSERespWriter) Write(data []byte) (int, error) {
return len(data), nil return len(data), nil
} }
func (w *mockSSERespWriter) WriteString(str string) (int, error) {
return w.Write([]byte(str))
}
func (w *mockSSERespWriter) ReadBytes() []byte { func (w *mockSSERespWriter) ReadBytes() []byte {
return <-w.writeNotification return <-w.writeNotification
} }
@ -156,7 +160,6 @@ func TestProxySingleOrigin(t *testing.T) {
t.Run("testProxyHTTP", testProxyHTTP(proxy)) t.Run("testProxyHTTP", testProxyHTTP(proxy))
t.Run("testProxyWebsocket", testProxyWebsocket(proxy)) t.Run("testProxyWebsocket", testProxyWebsocket(proxy))
t.Run("testProxySSE", testProxySSE(proxy)) t.Run("testProxySSE", testProxySSE(proxy))
t.Run("testProxySSEAllData", testProxySSEAllData(proxy))
cancel() cancel()
} }
@ -276,17 +279,15 @@ func testProxySSE(proxy connection.OriginProxy) func(t *testing.T) {
// Regression test to guarantee that we always write the contents downstream even if EOF is reached without // Regression test to guarantee that we always write the contents downstream even if EOF is reached without
// hitting the delimiter // hitting the delimiter
func testProxySSEAllData(proxy *Proxy) func(t *testing.T) { func TestProxySSEAllData(t *testing.T) {
return func(t *testing.T) { eyeballReader := io.NopCloser(strings.NewReader("data\r\r"))
eyeballReader := io.NopCloser(strings.NewReader("data\r\r")) responseWriter := newMockSSERespWriter()
responseWriter := newMockSSERespWriter()
// responseWriter uses an unbuffered channel, so we call in a different go-routine // responseWriter uses an unbuffered channel, so we call in a different go-routine
go cfio.Copy(responseWriter, eyeballReader) go cfio.Copy(responseWriter, eyeballReader)
result := string(<-responseWriter.writeNotification) result := string(<-responseWriter.writeNotification)
require.Equal(t, "data\r\r", result) require.Equal(t, "data\r\r", result)
}
} }
func TestProxyMultipleOrigins(t *testing.T) { func TestProxyMultipleOrigins(t *testing.T) {