-
Notifications
You must be signed in to change notification settings - Fork 56
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
Showing
4 changed files
with
96 additions
and
2 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 |
---|---|---|
|
@@ -7,3 +7,5 @@ require ( | |
golang.org/x/sync v0.3.0 | ||
golang.org/x/text v0.13.0 | ||
) | ||
|
||
require go.uber.org/goleak v1.3.0 // indirect |
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,77 @@ | ||
// Copyright 2024 The go-zeromq Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package reaper | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-zeromq/zmq4" | ||
"go.uber.org/goleak" | ||
) | ||
|
||
// TestReaper does multiple rapid Dial/Close to check that connection reaper goroutines are not leaking. | ||
// In is in own package as goleak detects also threads from objects instantiated by init(). | ||
func TestReaper(t *testing.T) { | ||
defer goleak.VerifyNone(t) | ||
//assert := asserts.New(t) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
rep := zmq4.NewRep(ctx) | ||
ep := "ipc://@test.rep.socket" | ||
err := rep.Listen(ep) | ||
_ = err // assert.NoError(err) | ||
|
||
maxClients := 100 | ||
maxMsgs := 100 | ||
wgClients := &sync.WaitGroup{} | ||
wgServer := &sync.WaitGroup{} | ||
client := func() { | ||
defer wgClients.Done() | ||
for n := 0; n < maxMsgs; n++ { | ||
func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
req := zmq4.NewReq(ctx) | ||
err := req.Dial(ep) | ||
_ = err // assert.NoError(err) | ||
|
||
err = req.Close() | ||
_ = err // assert.NoError(err) | ||
}() | ||
} | ||
} | ||
server := func() { | ||
defer wgServer.Done() | ||
pong := zmq4.NewMsgString("pong") | ||
for { | ||
msg, err := rep.Recv() | ||
if errors.Is(err, context.Canceled) { | ||
break | ||
} | ||
// assert.NoError(err) | ||
if err != nil { | ||
break | ||
} | ||
_ = msg // assert.Equal(string(msg.Frames[0]), "ping") | ||
err = rep.Send(pong) | ||
_ = err // assert.NoError(err) | ||
} | ||
} | ||
|
||
wgServer.Add(1) | ||
go server() | ||
wgClients.Add(maxClients) | ||
for n := 0; n < maxClients; n++ { | ||
go client() | ||
} | ||
wgClients.Wait() | ||
cancel() | ||
wgServer.Wait() | ||
rep.Close() | ||
} |
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