Skip to content

Commit

Permalink
Merge pull request #2 from lytics/code_cov
Browse files Browse the repository at this point in the history
code coverage
  • Loading branch information
araddon authored May 28, 2018
2 parents d5f1e97 + 91c9345 commit 84d34e4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
18 changes: 9 additions & 9 deletions callgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
"testing"

"github.com/bmizerany/assert"
"github.com/stretchr/testify/assert"
)

func TestCompletion(t *testing.T) {
Expand All @@ -20,14 +20,14 @@ func TestCompletion(t *testing.T) {
op1 := cg.Add(1, &tsMsg{123, 5, "user", 1234567})
op2 := cg.Add(2, &tsMsg{123, 5, "user", 2222222})

assert.T(t, completed == 0)
assert.T(t, reslen == 0)
assert.Equal(t, 0, completed)
assert.Equal(t, 0, reslen)
op1.Finish(nil, nil)
assert.T(t, completed == 0)
assert.T(t, reslen == 0)
assert.Equal(t, 0, completed)
assert.Equal(t, 0, reslen)
op2.Finish(nil, nil)
assert.T(t, completed == 1)
assert.T(t, reslen == 2)
assert.Equal(t, 1, completed)
assert.Equal(t, 2, reslen)
}

func TestConcurrentDone(t *testing.T) {
Expand Down Expand Up @@ -60,8 +60,8 @@ func TestConcurrentDone(t *testing.T) {
wgstart.Done() //start all go routines at the same time.
wgend.Wait()

assert.T(t, completed == 1)
assert.T(t, reslen == 1000)
assert.Equal(t, 1, completed)
assert.Equal(t, 1000, reslen)
}

type tsMsg struct {
Expand Down
32 changes: 16 additions & 16 deletions opqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,22 @@ func (q *OpQueue) Dequeue() (*OpSet, bool) {
for {
if set, ok := q.dequeue(); ok {
return set, true
} else {
select {
case <-q.ctx.Done():
return nil, false
default:
}
//release the lock and wait until signaled. On awake we'll require the lock.
// After wait requires the lock we have to recheck the wait condition
// (calling q.dequeue), because it's possible that someone else
// drained the queue while, we were reacquiring the lock.
q.cond.Wait()
select {
case <-q.ctx.Done():
return nil, false
default:
}
}

select {
case <-q.ctx.Done():
return nil, false
default:
}
// release the lock and wait until signaled. On awake we'll require the lock.
// After wait requires the lock we have to recheck the wait condition
// (calling q.dequeue), because it's possible that someone else
// drained the queue while, we were reacquiring the lock.
q.cond.Wait()
select {
case <-q.ctx.Done():
return nil, false
default:
}
}
}
Expand Down
59 changes: 30 additions & 29 deletions opqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"testing"
"time"

"github.com/bmizerany/assert"
"github.com/stretchr/testify/assert"

"github.com/lytics/inflight/testutils"
)

Expand Down Expand Up @@ -38,42 +39,42 @@ func TestOpQueue(t *testing.T) {

{
err := opq.Enqueue(op1_1.Key, op1_1)
assert.T(t, err == nil)
assert.Equal(t, nil, err)
err = opq.Enqueue(op2_1.Key, op2_1)
assert.T(t, err == nil)
assert.Equal(t, nil, err)
err = opq.Enqueue(op1_2.Key, op1_2)
assert.T(t, err == nil)
assert.Equal(t, nil, err)
err = opq.Enqueue(op2_2.Key, op2_2)
assert.T(t, err == nil)
assert.Equal(t, nil, err)
assert.Equal(t, 2, opq.Len()) // only 2 keys
}

set1, open := opq.Dequeue()
assert.T(t, open)
assert.True(t, open)
set2, open := opq.Dequeue()
assert.T(t, open)
assert.True(t, open)

{
assert.T(t, len(set1.Ops()) == 2)
assert.T(t, len(set2.Ops()) == 2)
assert.Equal(t, 2, len(set1.Ops()))
assert.Equal(t, 2, len(set2.Ops()))
}

{
//The sets should be made of one item of each callgroup, so we'll
//have to complete both sets before we expect complete[1,2] to increment
assert.T(t, completed1 == 0)
assert.T(t, completed2 == 0)
assert.Equal(t, 0, completed1)
assert.Equal(t, 0, completed2)
set := set1.Ops()
set[0].Finish(nil, nil)
set[1].Finish(nil, nil)
assert.T(t, completed1 == 0)
assert.T(t, completed2 == 0)
assert.Equal(t, 0, completed1)
assert.Equal(t, 0, completed2)

set = set2.Ops()
set[0].Finish(nil, nil)
set[1].Finish(nil, nil)
assert.T(t, completed1 == 1)
assert.T(t, completed2 == 1)
set2.FinishAll(nil, nil)
assert.Equal(t, 1, completed1)
assert.Equal(t, 1, completed2)
}

}

func TestOpQueueClose(t *testing.T) {
Expand All @@ -88,16 +89,16 @@ func TestOpQueueClose(t *testing.T) {
for i := 0; i < 9; i++ {
op := cg1.Add(uint64(i), &tsMsg{i, i, "user", 2222222})
err := opq.Enqueue(op.Key, op)
assert.T(t, err == nil)
assert.Equal(t, nil, err)
}

timer := time.AfterFunc(5*time.Second, func() {
t.Fatalf("testcase timed out after 5 secs.")
})
for i := 0; i < 9; i++ {
set1, open := opq.Dequeue()
assert.T(t, open)
assert.Tf(t, len(set1.Ops()) == 1, " at loop:%v set1_len:%v", i, len(set1.Ops()))
assert.True(t, open)
assert.Equal(t, 1, len(set1.Ops()), " at loop:%v set1_len:%v", i, len(set1.Ops()))
}
timer.Stop()

Expand All @@ -106,10 +107,10 @@ func TestOpQueueClose(t *testing.T) {
opq.Close() // calling close should release the call to opq.Dequeue()
})
set1, open := opq.Dequeue() //this call should hang until we call Close above.
assert.T(t, open == false, "expect:false got:%v", open)
assert.T(t, set1 == nil)
assert.Equal(t, false, open)
assert.True(t, nil == set1)
rt := time.Since(st)
assert.Tf(t, rt >= 10*time.Millisecond, "we shouldn't have returned until Close was called: returned after:%v", rt)
assert.True(t, rt >= 10*time.Millisecond, "we shouldn't have returned until Close was called: returned after:%v", rt)

}

Expand Down Expand Up @@ -149,8 +150,8 @@ func TestOpQueueFullDepth(t *testing.T) {
})
for i := 0; i < succuess; i++ {
set1, open := opq.Dequeue()
assert.T(t, open)
assert.Tf(t, len(set1.Ops()) == 1, " at loop:%v set1_len:%v", i, len(set1.Ops()))
assert.True(t, open)
assert.Equal(t, 1, len(set1.Ops()), " at loop:%v set1_len:%v", i, len(set1.Ops()))
}
timer.Stop()
}
Expand Down Expand Up @@ -194,8 +195,8 @@ func TestOpQueueFullWidth(t *testing.T) {

found := 0
set1, open := opq.Dequeue()
assert.T(t, open)
assert.Tf(t, len(set1.Ops()) == 10, " at loop:%v set1_len:%v", succuess, len(set1.Ops())) // max width is 10, so we should get 10 in the first batch
assert.True(t, open)
assert.Equal(t, 10, len(set1.Ops()), " at loop:%v set1_len:%v", succuess, len(set1.Ops())) // max width is 10, so we should get 10 in the first batch
found += len(set1.Ops())

timer.Stop()
Expand Down Expand Up @@ -267,7 +268,7 @@ func TestOpQueueForRaceDetection(t *testing.T) {
return
default:
}
assert.T(t, open)
assert.True(t, open)
dequeueCnt.IncrBy(len(set1.Ops()))
if len(set1.Ops()) > 1 {
mergeCnt.Incr()
Expand Down

0 comments on commit 84d34e4

Please sign in to comment.