Skip to content

Commit

Permalink
fix: stable testing
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza committed Oct 26, 2024
1 parent f3e9725 commit 57807fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestLog(t *testing.T) {
Level: slog.LevelInfo,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
a.Value = slog.TimeValue(time.Unix(0, 0))
a.Value = slog.TimeValue(time.Unix(0, 0).UTC())
}
return a
},
Expand All @@ -23,5 +23,5 @@ func TestLog(t *testing.T) {
log.DebugContext(context.TODO(), "world")
ResetLog()
log.InfoContext(context.TODO(), "other")
assertEqual(t, buff.String(), "time=1970-01-01T01:00:00.000+01:00 level=INFO msg=hello\n")
assertEqual(t, buff.String(), "time=1970-01-01T00:00:00.000Z level=INFO msg=hello\n")
}
39 changes: 35 additions & 4 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"

"github.com/automerge/automerge-go"
)
Expand All @@ -26,6 +26,28 @@ func TestServe_empty_request_body(t *testing.T) {
assertEqual(t, rw.Body.String(), "{\"event\":\"sync\",\"data\":\"QgAAAQAAAA==\"}\n")
}

type wrappedRw struct {
http.ResponseWriter
lineWaiter *sync.WaitGroup
}

func (w *wrappedRw) Write(data []byte) (int, error) {
n, err := w.ResponseWriter.Write(data)
for _, b := range data {
if b == '\n' {
w.lineWaiter.Done()
}
}
return n, err
}

func (w *wrappedRw) Flush() {
w.ResponseWriter.(http.Flusher).Flush()
}

var _ http.ResponseWriter = (*wrappedRw)(nil)
var _ http.Flusher = (*wrappedRw)(nil)

func TestServe_exchange(t *testing.T) {
t.Parallel()
sd := NewSharedDoc(automerge.New())
Expand All @@ -34,12 +56,21 @@ func TestServe_exchange(t *testing.T) {
assertEqual(t, sd.Doc().RootMap().Set("a", "b"), nil)
_, _ = sd.Doc().Commit("change")

rw := httptest.NewRecorder()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// The server will keep the connection open continuously writing whatever it may get in the future. So for the purpose of our test
// we need to know when we should cancel it once the required lines have been sent. We expect only 2 messages to be sent.
lineWaiter := new(sync.WaitGroup)
lineWaiter.Add(2)
go func() {
lineWaiter.Wait()
cancel()
}()
req := httptest.NewRequestWithContext(ctx, http.MethodPut, "/", io.NopCloser(strings.NewReader("{\"event\":\"sync\",\"data\":\"QgAAAQAAAA==\"}\n")))
assertEqual(t, sd.ServeChanges(rw, req), nil)
rw := httptest.NewRecorder()
assertEqual(t, sd.ServeChanges(&wrappedRw{lineWaiter: lineWaiter, ResponseWriter: rw}, req), nil)

assertEqual(t, rw.Result().StatusCode, http.StatusOK)
assertEqual(t, rw.Result().Header, map[string][]string{
"Content-Type": {ContentType},
Expand Down

0 comments on commit 57807fe

Please sign in to comment.