Skip to content

Commit

Permalink
chore: refactor RoutingBehaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
iand committed Oct 16, 2023
1 parent 0244dff commit 0465e06
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 98 deletions.
26 changes: 24 additions & 2 deletions internal/coord/behaviour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package coord

import (
"context"
"testing"
)

type RecordingSM[E any, S any] struct {
State S
Received E
Received []E
}

func NewRecordingSM[E any, S any](response S) *RecordingSM[E, S] {
Expand All @@ -16,6 +17,27 @@ func NewRecordingSM[E any, S any](response S) *RecordingSM[E, S] {
}

func (r *RecordingSM[E, S]) Advance(ctx context.Context, e E) S {
r.Received = e
r.Received = append(r.Received, e)
return r.State
}

func (r *RecordingSM[E, S]) first() E {
if len(r.Received) == 0 {
var zero E
return zero
}
return r.Received[0]
}

func DrainBehaviour[I BehaviourEvent, O BehaviourEvent](t *testing.T, ctx context.Context, b Behaviour[I, O]) {
for {
select {
case <-b.Ready():
b.Perform(ctx)
case <-ctx.Done():
t.Fatal("context cancelled while draining behaviour")
default:
return
}
}
}
Loading

0 comments on commit 0465e06

Please sign in to comment.