Skip to content

Commit

Permalink
Try to make sure the Receiver starts before we send events
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
Doug Davis committed Oct 27, 2023
1 parent f5c7061 commit 6ba9e47
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
22 changes: 16 additions & 6 deletions v2/client/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ import (
func SendReceive(t *testing.T, protocolFactory func() interface{}, in event.Event, outAssert func(e event.Event), opts ...client.Option) {
t.Helper()
pf := protocolFactory()
c, err := client.New(pf, opts...)

// Create a sender and receiver client since we can't assume it's safe
// to use the same one for both roles

sender, err := client.New(pf, opts...)
require.NoError(t, err)

receiver, err := client.New(pf, opts...)
require.NoError(t, err)

wg := sync.WaitGroup{}
wg.Add(2)

// Give time for Kafka client protocol to get setup
time.Sleep(2 * time.Second)
receiverReady := make(chan bool)

go func() {
ctx, cancel := context.WithCancel(context.TODO())
Expand All @@ -42,7 +49,8 @@ func SendReceive(t *testing.T, protocolFactory func() interface{}, in event.Even
wg.Done()
}(inCh)
go func(channel chan event.Event) {
err := c.StartReceiver(ctx, func(e event.Event) {
receiverReady <- true
err := receiver.StartReceiver(ctx, func(e event.Event) {
channel <- e
})
if err != nil {
Expand All @@ -53,12 +61,14 @@ func SendReceive(t *testing.T, protocolFactory func() interface{}, in event.Even
outAssert(e)
}()

// Give time for the receiever to start
// Wait for receiver to be setup. Not 100% perefect but the channel + the
// sleep should do it
<-receiverReady
time.Sleep(2 * time.Second)

go func() {
defer wg.Done()
err := c.Send(context.Background(), in)
err := sender.Send(context.Background(), in)
require.NoError(t, err)
}()

Expand Down
10 changes: 10 additions & 0 deletions v2/protocol/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ func SendReceive(t *testing.T, ctx context.Context, in binding.Message, s protoc
wg := sync.WaitGroup{}
wg.Add(2)

// Used to try to make sure the receiver is ready before we start to
// send events
wait := make(chan bool)

go func() {
defer wg.Done()
wait <- true
out, result := r.Receive(ctx)
if !protocol.IsACK(result) {
require.NoError(t, result)
Expand All @@ -36,6 +41,11 @@ func SendReceive(t *testing.T, ctx context.Context, in binding.Message, s protoc
require.NoError(t, finishErr)
}()

// Wait until receiver thread starts, and then wait a second to
// give the "Receive" call a chance to start (finger's crossed)
<-wait
time.Sleep(time.Second)

go func() {
defer wg.Done()
mx := sync.Mutex{}
Expand Down

0 comments on commit 6ba9e47

Please sign in to comment.