Skip to content

Commit

Permalink
fix race in simconn_test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Feb 6, 2025
1 parent 0fc0688 commit b8444b9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions p2p/net/simconn/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
// PerfectRouter is a router that has no latency or jitter and can route to
// every node
type PerfectRouter struct {
mu sync.Mutex
nodes map[net.Addr]*SimConn
}

// SendPacket implements Router.
func (r *PerfectRouter) SendPacket(deadline time.Time, p Packet) error {
r.mu.Lock()
defer r.mu.Unlock()
conn, ok := r.nodes[p.To]
if !ok {
return errors.New("unknown destination")
Expand Down
4 changes: 2 additions & 2 deletions p2p/net/simconn/simconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func TestSimConnDeadlinesWithLatency(t *testing.T) {
})

t.Run("read fails after deadline", func(t *testing.T) {
defer reset()
// Set a short deadline
deadline := time.Now().Add(50 * time.Millisecond) // Less than router latency
err := conn2.SetReadDeadline(deadline)
Expand All @@ -239,15 +240,14 @@ func TestSimConnDeadlinesWithLatency(t *testing.T) {
go func() {
defer wg.Done()
// Send data after setting deadline
_, err = conn1.WriteTo([]byte("test"), addr2)
_, err := conn1.WriteTo([]byte("test"), addr2)
require.NoError(t, err)
}()

// Read should fail due to deadline
buf := make([]byte, 1024)
_, _, err = conn2.ReadFrom(buf)
require.ErrorIs(t, err, ErrDeadlineExceeded)
reset()
})
}

Expand Down

0 comments on commit b8444b9

Please sign in to comment.