2018-09-19 19:56:50 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-09-26 14:42:02 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-09-19 19:56:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEventer(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
e := NewEventer(ctx, testlog.HCLogger(t))
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
events := []*drivers.TaskEvent{
|
2018-09-19 19:56:50 +00:00
|
|
|
{
|
|
|
|
TaskID: "a",
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskID: "b",
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TaskID: "c",
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
|
|
|
consumer1, err := e.TaskEvents(ctx1)
|
2018-09-19 19:56:50 +00:00
|
|
|
require.NoError(err)
|
2018-09-30 03:03:49 +00:00
|
|
|
ctx2 := (context.Background())
|
|
|
|
consumer2, err := e.TaskEvents(ctx2)
|
2018-09-19 19:56:50 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
var buffer1, buffer2 []*drivers.TaskEvent
|
2018-09-19 19:56:50 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-09-30 03:03:49 +00:00
|
|
|
var i int
|
|
|
|
for event := range consumer1 {
|
|
|
|
i++
|
2018-09-19 19:56:50 +00:00
|
|
|
buffer1 = append(buffer1, event)
|
2018-09-30 03:03:49 +00:00
|
|
|
if i == 3 {
|
|
|
|
break
|
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-09-30 03:03:49 +00:00
|
|
|
var i int
|
|
|
|
for event := range consumer2 {
|
|
|
|
i++
|
2018-09-19 19:56:50 +00:00
|
|
|
buffer2 = append(buffer2, event)
|
2018-09-30 03:03:49 +00:00
|
|
|
if i == 3 {
|
|
|
|
break
|
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, event := range events {
|
2018-09-30 03:03:49 +00:00
|
|
|
require.NoError(e.EmitEvent(event))
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
require.Exactly(events, buffer1)
|
|
|
|
require.Exactly(events, buffer2)
|
2018-09-30 03:03:49 +00:00
|
|
|
cancel1()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
require.Equal(1, len(e.consumers))
|
|
|
|
|
|
|
|
require.NoError(e.EmitEvent(&drivers.TaskEvent{}))
|
|
|
|
ev, ok := <-consumer1
|
|
|
|
require.Nil(ev)
|
|
|
|
require.False(ok)
|
|
|
|
ev, ok = <-consumer2
|
|
|
|
require.NotNil(ev)
|
|
|
|
require.True(ok)
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
require.Zero(len(e.consumers))
|
|
|
|
require.Error(e.EmitEvent(&drivers.TaskEvent{}))
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|