Skip to content

Commit

Permalink
Rework test to better simulate client crash
Browse files Browse the repository at this point in the history
Added context with cancel and then cancel context, queue has default
timeout of 60 seconds

Signed-off-by: Mercedes Coyle <mercedes@sensu.io>
  • Loading branch information
mercul3s committed Feb 2, 2018
1 parent 998ceef commit bb85eba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion backend/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
queuePrefix = "queue"
workPrefix = "work"
inFlightPrefix = "inflight"
itemTimeout = 60 * time.Second
)

var (
Expand All @@ -37,7 +38,7 @@ type Queue struct {
}

// New returns an instance of Queue.
func New(name string, client *clientv3.Client, itemTimeout time.Duration) *Queue {
func New(name string, client *clientv3.Client) *Queue {
queue := &Queue{
client: client,
work: queueKeyBuilder.Build(name, workPrefix),
Expand Down
32 changes: 19 additions & 13 deletions backend/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestEnqueue(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testenq", client, time.Second)
queue := New("testenq", client)
err = queue.Enqueue(context.Background(), "test item")
assert.NoError(t, err)
}
Expand All @@ -32,7 +32,7 @@ func TestDequeueSingleItem(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testdeq", client, time.Second)
queue := New("testdeq", client)
err = queue.Enqueue(context.Background(), "test single item dequeue")
require.NoError(t, err)

Expand All @@ -54,7 +54,7 @@ func TestDequeueFIFO(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testfifo", client, time.Second)
queue := New("testfifo", client)
items := []string{"hello", "there", "world", "asdf", "fjdksl", "lalalal"}

for _, item := range items {
Expand All @@ -80,7 +80,7 @@ func TestDequeueParallel(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testparallel", client, time.Second)
queue := New("testparallel", client)
items := map[string]struct{}{
"hello": struct{}{},
"there": struct{}{},
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestNack(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testnack", client, time.Second)
queue := New("testnack", client)
err = queue.Enqueue(context.Background(), "test item")
require.NoError(t, err)

Expand All @@ -154,7 +154,7 @@ func TestAck(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testack", client, time.Second)
queue := New("testack", client)
err = queue.Enqueue(context.Background(), "test item")
require.NoError(t, err)

Expand All @@ -179,7 +179,7 @@ func TestOnce(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testonce", client, time.Second)
queue := New("testonce", client)

err = queue.Enqueue(context.Background(), "test item")
require.NoError(t, err)
Expand All @@ -204,24 +204,30 @@ func TestNackExpired(t *testing.T) {
client, err := e.NewClient()
require.NoError(t, err)

queue := New("testexpired", client, time.Second)
queue := New("testexpired", client)
queue.itemTimeout = 2 * time.Second

err = queue.Enqueue(context.Background(), "test item")
ctx, cancel := context.WithCancel(context.Background())

err = queue.Enqueue(ctx, "test item")
require.NoError(t, err)

item, err := queue.Dequeue(context.Background())
item, err := queue.Dequeue(ctx)
require.NoError(t, err)

// close the first client
client.Close()
// wait to make sure the item has timed out
time.Sleep(2 * time.Second)
cancel()

// create a new client and queue
newClient, err := e.NewClient()
require.NoError(t, err)

newQueue := New("testexpired", newClient, time.Second)
// wait to make sure the item has timed out
time.Sleep(2 * time.Second)

newQueue := New("testexpired", newClient)
newQueue.itemTimeout = 2 * time.Second

// nacked item should go back in the work queue lane
item, err = newQueue.Dequeue(context.Background())
Expand Down

0 comments on commit bb85eba

Please sign in to comment.