Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
baaf54499d |
No files matched your search
@@ -145,9 +145,19 @@ type testServer struct {
|
|||||||
handlers *handlers.Handlers
|
handlers *handlers.Handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
// maxMessageSize is MAX_MESSAGE_SIZE on every test server, the
|
const (
|
||||||
// setting's default.
|
// maxMessageSize is MAX_MESSAGE_SIZE on every test server, the
|
||||||
const maxMessageSize = 4096
|
// setting's default.
|
||||||
|
maxMessageSize = 4096
|
||||||
|
|
||||||
|
// loginRateLimit is LOGIN_RATE_LIMIT on every test server, the
|
||||||
|
// setting's default.
|
||||||
|
loginRateLimit = 1
|
||||||
|
|
||||||
|
// loginRateBurst is LOGIN_RATE_BURST on every test server, the
|
||||||
|
// setting's default.
|
||||||
|
loginRateBurst = 5
|
||||||
|
)
|
||||||
|
|
||||||
func newTestServer(
|
func newTestServer(
|
||||||
t *testing.T,
|
t *testing.T,
|
||||||
@@ -188,7 +198,7 @@ func newTestServerWith(
|
|||||||
logger.New,
|
logger.New,
|
||||||
// Built here rather than by config.New, which writes
|
// Built here rather than by config.New, which writes
|
||||||
// the global viper instance, so servers start in
|
// the global viper instance, so servers start in
|
||||||
// parallel. Settings left out take their defaults.
|
// parallel.
|
||||||
func() *config.Config {
|
func() *config.Config {
|
||||||
return &config.Config{
|
return &config.Config{
|
||||||
DBURL: dbURL,
|
DBURL: dbURL,
|
||||||
@@ -201,6 +211,8 @@ func newTestServerWith(
|
|||||||
MessageMaxAge: thirtyDays,
|
MessageMaxAge: thirtyDays,
|
||||||
QueueMaxAge: thirtyDays,
|
QueueMaxAge: thirtyDays,
|
||||||
SessionIdleTimeout: thirtyDays,
|
SessionIdleTimeout: thirtyDays,
|
||||||
|
LoginRateLimit: loginRateLimit,
|
||||||
|
LoginRateBurst: loginRateBurst,
|
||||||
TrustedProxies: trusted,
|
TrustedProxies: trusted,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -3029,18 +3041,18 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
|||||||
longTimeout = 10
|
longTimeout = 10
|
||||||
|
|
||||||
// shortTimeout, in seconds, is the timeout of the
|
// shortTimeout, in seconds, is the timeout of the
|
||||||
// requests that wait it out.
|
// request that waits it out.
|
||||||
shortTimeout = 1
|
shortTimeout = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// queued is whether a message is queued before the request,
|
// queued is whether a message is queued before the request,
|
||||||
// whose timeout is in seconds. failingRead is as for
|
// whose timeout is in seconds. failingRead and notified are
|
||||||
// getMessagesFailingRead. wantReads is how many times the
|
// as for getMessagesFailingRead. wantReads is how many times
|
||||||
// handler reads the queue: once if it answers at once,
|
// the handler reads the queue: once if it answers at once,
|
||||||
// twice if it waits first.
|
// twice if it waits first.
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
queued bool
|
queued, notified bool
|
||||||
timeout, failingRead int
|
timeout, failingRead int
|
||||||
wantReads, wantStatus int
|
wantReads, wantStatus int
|
||||||
}{
|
}{
|
||||||
@@ -3061,9 +3073,9 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
|||||||
wantReads: 1, wantStatus: http.StatusInternalServerError,
|
wantReads: 1, wantStatus: http.StatusInternalServerError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "read after the wait failed", timeout: shortTimeout,
|
name: "read after the wait failed", notified: true,
|
||||||
failingRead: 2, wantReads: 2,
|
timeout: handlers.MaxLongPollTimeout, failingRead: 2,
|
||||||
wantStatus: http.StatusInternalServerError,
|
wantReads: 2, wantStatus: http.StatusInternalServerError,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3095,7 +3107,7 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, reads := tserver.getMessagesFailingRead(
|
status, reads := tserver.getMessagesFailingRead(
|
||||||
token, lastID, tc.timeout, tc.failingRead,
|
token, sessionID, lastID, tc.timeout, tc.failingRead, tc.notified,
|
||||||
)
|
)
|
||||||
if status != tc.wantStatus {
|
if status != tc.wantStatus {
|
||||||
t.Errorf("status %d, want %d", status, tc.wantStatus)
|
t.Errorf("status %d, want %d", status, tc.wantStatus)
|
||||||
@@ -3112,14 +3124,17 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getMessagesFailingRead sends GET /api/v1/messages for the
|
// getMessagesFailingRead sends GET /api/v1/messages for the
|
||||||
// client token with timeout and after afterID. The handler's
|
// client token of the session sessionID, with timeout and
|
||||||
// read of the queue numbered failingRead, counting from 1,
|
// after afterID. The handler's read of the queue numbered
|
||||||
// fails; with 0, none does. It returns the response status
|
// failingRead, counting from 1, fails; with 0, none does.
|
||||||
// and how many times the handler read the queue.
|
// With notified, it notifies the broker once the request
|
||||||
|
// waits. It returns the response status and how many times
|
||||||
|
// the handler read the queue.
|
||||||
func (tserver *testServer) getMessagesFailingRead(
|
func (tserver *testServer) getMessagesFailingRead(
|
||||||
token string,
|
token string,
|
||||||
afterID int64,
|
sessionID, afterID int64,
|
||||||
timeout, failingRead int,
|
timeout, failingRead int,
|
||||||
|
notified bool,
|
||||||
) (int, int) {
|
) (int, int) {
|
||||||
tserver.t.Helper()
|
tserver.t.Helper()
|
||||||
|
|
||||||
@@ -3141,20 +3156,45 @@ func (tserver *testServer) getMessagesFailingRead(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
resp, err := doRequestAuth(
|
var (
|
||||||
tserver.t, http.MethodGet,
|
status int
|
||||||
tserver.url(fmt.Sprintf(
|
err error
|
||||||
"%s?timeout=%d&after=%d", apiMessages, timeout, afterID,
|
|
||||||
)),
|
|
||||||
token, nil,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
answered := make(chan struct{})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(answered)
|
||||||
|
|
||||||
|
var resp *http.Response
|
||||||
|
|
||||||
|
resp, err = doRequestAuth(
|
||||||
|
tserver.t, http.MethodGet,
|
||||||
|
tserver.url(fmt.Sprintf(
|
||||||
|
"%s?timeout=%d&after=%d", apiMessages, timeout, afterID,
|
||||||
|
)),
|
||||||
|
token, nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
status = resp.StatusCode
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if notified {
|
||||||
|
tserver.waitForLongPoll(sessionID)
|
||||||
|
tserver.broker.Notify(sessionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
<-answered
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tserver.t.Fatalf("get messages: %v", err)
|
tserver.t.Fatalf("get messages: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = resp.Body.Close()
|
return status, reads
|
||||||
|
|
||||||
return resp.StatusCode, reads
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestLongPollRemovesWaitChannelWhenClientGivesUp checks that
|
// TestLongPollRemovesWaitChannelWhenClientGivesUp checks that
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ import (
|
|||||||
"sneak.berlin/go/neoirc/internal/db"
|
"sneak.berlin/go/neoirc/internal/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MaxLongPollTimeout is the longest HandleGetMessages waits, in
|
||||||
|
// seconds.
|
||||||
|
const MaxLongPollTimeout = maxLongPollTimeout
|
||||||
|
|
||||||
// SetPollMessages makes HandleGetMessages read the client
|
// SetPollMessages makes HandleGetMessages read the client
|
||||||
// output queue with poll in place of Database.PollMessages.
|
// output queue with poll in place of Database.PollMessages.
|
||||||
func (hdlr *Handlers) SetPollMessages(
|
func (hdlr *Handlers) SetPollMessages(
|
||||||
|
|||||||
Reference in new issue
Block a user