1
0
Fork 0

Better queue unit testing

master
Philip O'Toole 2 years ago
parent d52946aac1
commit 3b9a73c1ac

@ -19,6 +19,9 @@ type Queue struct {
done chan struct{}
closed chan struct{}
flush chan struct{}
// Whitebox unit-testing
numTimeouts int
}
// New returns a instance of a Queue
@ -96,6 +99,7 @@ func (q *Queue) run() {
writeFn()
}
case <-timer.C:
q.numTimeouts++
writeFn()
case <-q.flush:
writeFn()

@ -64,6 +64,9 @@ func Test_NewQueueWriteBatchSizeMulti(t *testing.T) {
if len(stmts) != 5 {
t.Fatalf("received wrong length slice")
}
if q.numTimeouts != 0 {
t.Fatalf("queue timeout expired?")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for first statements")
}
@ -79,6 +82,9 @@ func Test_NewQueueWriteBatchSizeMulti(t *testing.T) {
if len(stmts) < 5 {
t.Fatalf("received too-short slice")
}
if q.numTimeouts != 0 {
t.Fatalf("queue timeout expired?")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for second statements")
}
@ -100,6 +106,9 @@ func Test_NewQueueWriteTimeout(t *testing.T) {
if stmts[0].Sql != "SELECT * FROM foo" {
t.Fatalf("received wrong SQL")
}
if q.numTimeouts != 1 {
t.Fatalf("queue timeout didn't expire")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for statement")
}
@ -122,6 +131,9 @@ func Test_NewQueueWriteTimeoutMulti(t *testing.T) {
if stmts[0].Sql != "SELECT * FROM foo" {
t.Fatalf("received wrong SQL")
}
if q.numTimeouts != 1 {
t.Fatalf("queue timeout didn't expire")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for first statement")
}
@ -137,6 +149,9 @@ func Test_NewQueueWriteTimeoutMulti(t *testing.T) {
if stmts[0].Sql != "SELECT * FROM foo" {
t.Fatalf("received wrong SQL")
}
if q.numTimeouts != 2 {
t.Fatalf("queue timeout didn't expire")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for second statement")
}
@ -145,7 +160,7 @@ func Test_NewQueueWriteTimeoutMulti(t *testing.T) {
// Test_NewQueueWriteTimeoutBatch ensures that timer expiring
// followed by a batch, works fine.
func Test_NewQueueWriteTimeoutBatch(t *testing.T) {
q := New(1024, 2, 2*time.Second)
q := New(1024, 2, 1*time.Second)
defer q.Close()
if err := q.Write(testStmt); err != nil {
@ -160,6 +175,9 @@ func Test_NewQueueWriteTimeoutBatch(t *testing.T) {
if stmts[0].Sql != "SELECT * FROM foo" {
t.Fatalf("received wrong SQL")
}
if q.numTimeouts != 1 {
t.Fatalf("queue timeout didn't expire")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for statement")
}
@ -172,14 +190,17 @@ func Test_NewQueueWriteTimeoutBatch(t *testing.T) {
}
select {
case stmts := <-q.C:
// Should happen before the timeout expires.
if len(stmts) != 2 {
t.Fatalf("received wrong length slice")
}
if stmts[0].Sql != "SELECT * FROM foo" {
t.Fatalf("received wrong SQL")
}
case <-time.NewTimer(500 * time.Millisecond).C:
// Should happen before the timeout expires.
if q.numTimeouts != 1 {
t.Fatalf("queue timeout expired?")
}
case <-time.NewTimer(5 * time.Second).C:
t.Fatalf("timed out waiting for statement")
}
}

Loading…
Cancel
Save