Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8c7ac665d |
@@ -490,6 +490,98 @@ func TestSecondConnectStopsFirstPollLoop(t *testing.T) {
|
||||
app.mu.Unlock()
|
||||
}
|
||||
|
||||
// TestSecondConnectIgnoresHeldPoll holds a poll of the earlier
|
||||
// connection at its server while a second /connect runs, then
|
||||
// answers it with a cursor and a message. It fails if that answer
|
||||
// moves the cursor or shows the message.
|
||||
func TestSecondConnectIgnoresHeldPoll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const session = `{"id": 1, "nick": "erin"}`
|
||||
|
||||
const answer = `{"messages": [{"command": "PRIVMSG", "from": "bob",
|
||||
"to": "#chan", "body": ["hello"]}], "last_id": 42}`
|
||||
|
||||
// The first server sends on polled when the poll arrives, and
|
||||
// answers it once release is closed.
|
||||
polled := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
first := httptest.NewServer(http.HandlerFunc(
|
||||
func(writer http.ResponseWriter, request *http.Request) {
|
||||
select {
|
||||
case polled <- struct{}{}:
|
||||
case <-request.Context().Done():
|
||||
return
|
||||
}
|
||||
|
||||
select {
|
||||
case <-release:
|
||||
_, _ = io.WriteString(writer, answer)
|
||||
case <-request.Context().Done():
|
||||
}
|
||||
},
|
||||
))
|
||||
t.Cleanup(first.Close)
|
||||
t.Cleanup(first.CloseClientConnections)
|
||||
|
||||
// The second server answers every request at once with the
|
||||
// session. A poll answered with it has no cursor and no messages,
|
||||
// so the second poll loop never moves the cursor.
|
||||
second := newServer(t, session)
|
||||
|
||||
// The test runs the earlier connection's poll loop itself, so
|
||||
// that it can wait for the loop to end.
|
||||
stop := make(chan struct{})
|
||||
app := &App{
|
||||
ui: NewUI(),
|
||||
client: api.NewClient(first.URL),
|
||||
nick: testNick,
|
||||
connected: true,
|
||||
stopPoll: stop,
|
||||
}
|
||||
runUI(t, app.ui)
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
app.pollLoop(stop)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-polled:
|
||||
case <-time.After(eventLoopTimeout):
|
||||
t.Fatalf("the first server got no poll within %v", eventLoopTimeout)
|
||||
}
|
||||
|
||||
app.cmdConnect(second.URL)
|
||||
close(release)
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(eventLoopTimeout):
|
||||
t.Fatalf("the earlier poll loop did not end within %v", eventLoopTimeout)
|
||||
}
|
||||
|
||||
app.mu.Lock()
|
||||
cursor := app.lastQID
|
||||
app.mu.Unlock()
|
||||
|
||||
if cursor != 0 {
|
||||
t.Errorf("the earlier connection's answer moved the cursor to %d", cursor)
|
||||
}
|
||||
|
||||
lines, _ := bufferState(app.ui, testChannel)
|
||||
if lines != 0 {
|
||||
t.Errorf("the earlier connection's message was shown in %s", testChannel)
|
||||
}
|
||||
|
||||
// Stop the second poll loop as /quit does, without stopping the UI.
|
||||
app.mu.Lock()
|
||||
close(app.stopPoll)
|
||||
app.mu.Unlock()
|
||||
}
|
||||
|
||||
// TestStartUp runs the client's start-up path on a simulation
|
||||
// screen. It fails unless the event loop starts and the status
|
||||
// window gets two lines: first the one that holds "Welcome to
|
||||
|
||||
Reference in New Issue
Block a user