This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from iand/routing-probe
routing: integrate probe state machine
- Loading branch information
Showing
8 changed files
with
507 additions
and
59 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
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
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
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
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,54 @@ | ||
package kademlia | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type NullSM[E any, S any] struct{} | ||
|
||
func (NullSM[E, S]) Advance(context.Context, E) S { | ||
var v S | ||
return v | ||
} | ||
|
||
type RecordingSM[E any, S any] struct { | ||
State S | ||
Received E | ||
} | ||
|
||
func NewRecordingSM[E any, S any](response S) *RecordingSM[E, S] { | ||
return &RecordingSM[E, S]{ | ||
State: response, | ||
} | ||
} | ||
|
||
func (r *RecordingSM[E, S]) Advance(ctx context.Context, e E) S { | ||
r.Received = e | ||
return r.State | ||
} | ||
|
||
// expectBehaviourEvent selects on a behaviour's ready channel until it becomes ready and then checks the perform | ||
// mehtod for the expected event type. Unexpected events are ignored and selecting resumes. | ||
// The function returns when an event matching the type of expected is received or when the context is cancelled. | ||
func expectBehaviourEvent[I DhtEvent, O DhtEvent](t *testing.T, ctx context.Context, b Behaviour[I, O], expected O) (O, error) { | ||
t.Helper() | ||
for { | ||
select { | ||
case <-b.Ready(): | ||
ev, ok := b.Perform(ctx) | ||
if !ok { | ||
continue | ||
} | ||
t.Logf("saw event: %T\n", ev) | ||
if reflect.TypeOf(ev) == reflect.TypeOf(expected) { | ||
return ev, nil | ||
} | ||
case <-ctx.Done(): | ||
var v O | ||
return v, fmt.Errorf("test deadline exceeded") | ||
} | ||
} | ||
} |
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
Oops, something went wrong.