harness-drone/pubsub/sub_test.go

86 lines
1.8 KiB
Go
Raw Permalink Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
2019-02-28 07:07:13 +00:00
// +build !oss
2019-02-19 23:56:41 +00:00
package pubsub
import (
"testing"
"github.com/drone/drone/core"
)
func nop(*core.Message) {}
func TestSubscription_publish(t *testing.T) {
s := &subscriber{
handler: make(chan *core.Message, 5),
quit: make(chan struct{}),
}
e := new(core.Message)
s.publish(e)
if got, want := len(s.handler), 1; got != want {
t.Errorf("Want buffered channel size %d, got %d", want, got)
}
if got, want := <-s.handler, e; got != want {
t.Errorf("Want event received from channel")
}
if got, want := len(s.handler), 0; got != want {
t.Errorf("Want buffered channel size %d, got %d", want, got)
}
}
func TestSubscription_buffer(t *testing.T) {
s := &subscriber{
handler: make(chan *core.Message, 1),
quit: make(chan struct{}),
}
// the buffer size is 1 to simulate what happens
// if the subscriber cannot keep up with processing
// and the buffer fills up. In this case, events
// should be ignored until pending events are
// processed.
e := new(core.Message)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
if got, want := len(s.handler), 1; got != want {
t.Errorf("Want buffered channel size %d, got %d", want, got)
}
}
func TestSubscription_stop(t *testing.T) {
s := &subscriber{
handler: make(chan *core.Message, 1),
quit: make(chan struct{}),
}
if got, want := s.done, false; got != want {
t.Errorf("Want subscription open")
}
s.close()
if got, want := s.done, true; got != want {
t.Errorf("Want subscription closed")
}
// if the subscription is closed we should
// ignore any new events being published.
e := new(core.Message)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
s.publish(e)
}