2015-04-23 23:22:23 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
2015-05-05 08:04:20 +00:00
|
|
|
"sync"
|
2015-04-23 23:22:23 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/queue"
|
|
|
|
. "github.com/franela/goblin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuild(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
g.Describe("Queue", func() {
|
|
|
|
|
|
|
|
g.It("Should publish item", func() {
|
|
|
|
w1 := &queue.Work{}
|
|
|
|
w2 := &queue.Work{}
|
|
|
|
q := New()
|
|
|
|
q.Publish(w1)
|
|
|
|
q.Publish(w2)
|
|
|
|
g.Assert(len(q.items)).Equal(2)
|
|
|
|
g.Assert(len(q.itemc)).Equal(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should remove item", func() {
|
|
|
|
w1 := &queue.Work{}
|
|
|
|
w2 := &queue.Work{}
|
|
|
|
w3 := &queue.Work{}
|
|
|
|
q := New()
|
|
|
|
q.Publish(w1)
|
|
|
|
q.Publish(w2)
|
|
|
|
q.Publish(w3)
|
|
|
|
q.Remove(w2)
|
|
|
|
g.Assert(len(q.items)).Equal(2)
|
|
|
|
g.Assert(len(q.itemc)).Equal(2)
|
|
|
|
g.Assert(q.Pull()).Equal(w1)
|
|
|
|
g.Assert(q.Pull()).Equal(w3)
|
2015-04-23 23:32:46 +00:00
|
|
|
g.Assert(q.Remove(w2)).Equal(ErrNotFound)
|
2015-04-23 23:22:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should pull item", func() {
|
|
|
|
w1 := &queue.Work{}
|
|
|
|
w2 := &queue.Work{}
|
|
|
|
q := New()
|
2015-05-05 08:04:20 +00:00
|
|
|
c := new(closeNotifier)
|
2015-04-23 23:22:23 +00:00
|
|
|
q.Publish(w1)
|
|
|
|
q.Publish(w2)
|
|
|
|
g.Assert(q.Pull()).Equal(w1)
|
2015-05-05 08:04:20 +00:00
|
|
|
g.Assert(q.PullClose(c)).Equal(w2)
|
|
|
|
g.Assert(q.acks[w1]).Equal(struct{}{})
|
|
|
|
g.Assert(q.acks[w2]).Equal(struct{}{})
|
|
|
|
g.Assert(len(q.acks)).Equal(2)
|
2015-04-23 23:22:23 +00:00
|
|
|
})
|
|
|
|
|
2015-05-05 08:04:20 +00:00
|
|
|
g.It("Should cancel pulling item", func() {
|
2015-04-23 23:22:23 +00:00
|
|
|
q := New()
|
2015-05-05 08:04:20 +00:00
|
|
|
c := new(closeNotifier)
|
|
|
|
c.closec = make(chan bool, 1)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
go func() {
|
|
|
|
wg.Add(1)
|
|
|
|
g.Assert(q.PullClose(c) == nil).IsTrue()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
c.closec <- true
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2015-04-23 23:22:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should ack item", func() {
|
|
|
|
w := &queue.Work{}
|
2015-05-05 08:04:20 +00:00
|
|
|
c := new(closeNotifier)
|
2015-04-23 23:22:23 +00:00
|
|
|
q := New()
|
|
|
|
q.Publish(w)
|
2015-05-05 08:04:20 +00:00
|
|
|
g.Assert(q.PullClose(c)).Equal(w)
|
2015-04-23 23:22:23 +00:00
|
|
|
g.Assert(len(q.acks)).Equal(1)
|
|
|
|
g.Assert(q.Ack(w)).Equal(nil)
|
|
|
|
g.Assert(len(q.acks)).Equal(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get all items", func() {
|
|
|
|
q := New()
|
|
|
|
q.Publish(&queue.Work{})
|
|
|
|
q.Publish(&queue.Work{})
|
|
|
|
q.Publish(&queue.Work{})
|
|
|
|
g.Assert(len(q.Items())).Equal(3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
type closeNotifier struct {
|
|
|
|
closec chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *closeNotifier) CloseNotify() <-chan bool {
|
|
|
|
return c.closec
|
|
|
|
}
|