-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
989ea8d
commit a0e1f28
Showing
5 changed files
with
102 additions
and
77 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// TODO add documentation, coverage | ||
// Package events contains a simple p2p event system implementation, that simplifies asynchronous event flows in the | ||
// p2p module. The event subscriptions allow for event filtering, which eases the load on the event notification flow. | ||
package events |
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,94 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/p2p/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// generateEvents generates p2p events | ||
func generateEvents(count int) []Event { | ||
events := make([]Event, 0, count) | ||
|
||
for i := range count { | ||
var event Event | ||
|
||
if i%2 == 0 { | ||
event = PeerConnectedEvent{ | ||
PeerID: types.ID(fmt.Sprintf("peer-%d", i)), | ||
} | ||
} else { | ||
event = PeerDisconnectedEvent{ | ||
PeerID: types.ID(fmt.Sprintf("peer-%d", i)), | ||
} | ||
} | ||
|
||
events = append(events, event) | ||
} | ||
|
||
return events | ||
} | ||
|
||
func TestEvents_Subscribe(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
capturedEvents []Event | ||
|
||
events = generateEvents(10) | ||
subFn = func(e Event) bool { | ||
return e.Type() == PeerDisconnected | ||
} | ||
) | ||
|
||
// Create the events manager | ||
e := New() | ||
|
||
// Subscribe to events | ||
ch, unsubFn := e.Subscribe(subFn) | ||
defer unsubFn() | ||
|
||
// Listen for the events | ||
var wg sync.WaitGroup | ||
|
||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
|
||
timeout := time.After(5 * time.Second) | ||
|
||
for { | ||
select { | ||
case ev := <-ch: | ||
capturedEvents = append(capturedEvents, ev) | ||
|
||
if len(capturedEvents) == len(events)/2 { | ||
return | ||
} | ||
case <-timeout: | ||
return | ||
} | ||
} | ||
}() | ||
|
||
// Send out the events | ||
for _, ev := range events { | ||
e.Notify(ev) | ||
} | ||
|
||
wg.Wait() | ||
|
||
// Make sure the events were captured | ||
// and filtered properly | ||
require.Len(t, capturedEvents, len(events)/2) | ||
|
||
for _, ev := range capturedEvents { | ||
assert.Equal(t, ev.Type(), PeerDisconnected) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.