2018-10-02 19:40:27 +00:00
|
|
|
package eventer
|
2018-09-19 19:56:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-19 19:56:50 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2018-11-20 03:58:05 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2018-09-30 03:03:49 +00:00
|
|
|
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-11-20 03:58:05 +00:00
|
|
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
|
|
|
defer cancel1()
|
2018-09-30 03:03:49 +00:00
|
|
|
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-10-02 19:36:55 +00:00
|
|
|
if i == len(events) {
|
|
|
|
return
|
2018-09-30 03:03:49 +00:00
|
|
|
}
|
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-10-02 19:36:55 +00:00
|
|
|
if i == len(events) {
|
|
|
|
return
|
2018-09-30 03:03:49 +00:00
|
|
|
}
|
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-10-02 19:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventer_iterateConsumers(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-10-02 19:36:55 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
e := &Eventer{
|
|
|
|
events: make(chan *drivers.TaskEvent),
|
|
|
|
ctx: context.Background(),
|
|
|
|
logger: testlog.HCLogger(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := &drivers.TaskEvent{
|
|
|
|
TaskID: "a",
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
|
|
|
consumer, err := e.TaskEvents(ctx1)
|
|
|
|
require.NoError(err)
|
2018-09-30 03:03:49 +00:00
|
|
|
require.Equal(1, len(e.consumers))
|
|
|
|
|
2018-10-02 19:36:55 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ev1, ok := <-consumer
|
|
|
|
require.Exactly(ev, ev1)
|
|
|
|
require.True(ok)
|
|
|
|
}()
|
|
|
|
e.iterateConsumers(ev)
|
|
|
|
wg.Wait()
|
2018-09-30 03:03:49 +00:00
|
|
|
|
2018-10-02 19:36:55 +00:00
|
|
|
go func() {
|
|
|
|
cancel1()
|
|
|
|
e.iterateConsumers(ev)
|
|
|
|
}()
|
|
|
|
ev1, ok := <-consumer
|
|
|
|
require.False(ok)
|
|
|
|
require.Nil(ev1)
|
|
|
|
require.Equal(0, len(e.consumers))
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|