-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reflex/testmock: Add await consumer test util
- Loading branch information
1 parent
6898c0c
commit 80d135c
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package testmock | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/luno/jettison/jtest" | ||
|
||
"github.com/luno/reflex" | ||
) | ||
|
||
func AwaitConsumer(t *testing.T, cs reflex.CursorStore, consumerName string, eventID int64) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) | ||
t.Cleanup(cancel) | ||
|
||
for ctx.Err() == nil { | ||
val, err := cs.GetCursor(ctx, consumerName) | ||
jtest.RequireNil(t, err) | ||
|
||
eID := int64(0) | ||
if val != "" { | ||
eID, err = strconv.ParseInt(val, 10, 64) | ||
jtest.RequireNil(t, err) | ||
} | ||
|
||
if eID == eventID { | ||
break | ||
} | ||
|
||
time.Sleep(5 * time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package testmock_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/luno/jettison/jtest" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/luno/reflex" | ||
"github.com/luno/reflex/rpatterns" | ||
"github.com/luno/reflex/testmock" | ||
) | ||
|
||
func TestAwait(t *testing.T) { | ||
streamer := testmock.NewTestStreamer(t) | ||
defer streamer.Stop() | ||
|
||
streamer.InsertEvent(reflex.Event{ | ||
ID: "1", | ||
Type: reflexType(1), | ||
ForeignID: "9", | ||
Timestamp: time.Now(), | ||
}) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
cStore := rpatterns.MemCursorStore() | ||
|
||
s := "" | ||
result := &s | ||
consumer := reflex.NewConsumer("my-consumer", func(ctx context.Context, event *reflex.Event) error { | ||
*result = "Hello World" | ||
return nil | ||
}) | ||
|
||
spec := reflex.NewSpec(streamer.StreamFunc(), cStore, consumer, reflex.WithStreamToHead()) | ||
go func() { | ||
err := reflex.Run(ctx, spec) | ||
jtest.Require(t, reflex.ErrHeadReached, err) | ||
}() | ||
|
||
testmock.AwaitConsumer(t, cStore, "my-consumer", 1) | ||
|
||
require.Equal(t, "Hello World", *result) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters