-
Notifications
You must be signed in to change notification settings - Fork 57
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
120 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,101 @@ | ||
// 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 values created during init(). | ||
func TestReaper(t *testing.T) { | ||
defer goleak.VerifyNone(t) | ||
|
||
mu := &sync.Mutex{} | ||
errs := []error{} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
rep := zmq4.NewRep(ctx) | ||
ep := "ipc://@test.rep.socket" | ||
err := rep.Listen(ep) | ||
if err != nil { | ||
t.Fatal(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) | ||
if err != nil { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
errs = append(errs, err) | ||
return | ||
} | ||
|
||
err = req.Close() | ||
if err != nil { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
errs = append(errs, err) | ||
} | ||
}() | ||
} | ||
} | ||
server := func() { | ||
defer wgServer.Done() | ||
pong := zmq4.NewMsgString("pong") | ||
for { | ||
msg, err := rep.Recv() | ||
if errors.Is(err, context.Canceled) { | ||
break | ||
} | ||
if err != nil { | ||
break | ||
} | ||
if string(msg.Frames[0]) != "ping" { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
errs = append(errs, errors.New("unexpected message")) | ||
return | ||
} | ||
err = rep.Send(pong) | ||
if err != nil { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
errs = append(errs, err) | ||
} | ||
} | ||
} | ||
|
||
wgServer.Add(1) | ||
go server() | ||
wgClients.Add(maxClients) | ||
for n := 0; n < maxClients; n++ { | ||
go client() | ||
} | ||
wgClients.Wait() | ||
cancel() | ||
wgServer.Wait() | ||
rep.Close() | ||
for _, err := range errs { | ||
t.Fatal(err) | ||
} | ||
} |
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