MCPcopy
hub / github.com/nats-io/nats.go / TestSetPendingLimits

Function TestSetPendingLimits

test/sub_test.go:1342–1510  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1340}
1341
1342func TestSetPendingLimits(t *testing.T) {
1343 s := RunDefaultServer()
1344 defer s.Shutdown()
1345
1346 nc := NewDefaultConnection(t)
1347 defer nc.Close()
1348
1349 // Override default handler for test.
1350 nc.SetErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, _ error) {})
1351
1352 payload := []byte("hello")
1353 payloadLen := len(payload)
1354 toSend := 100
1355
1356 var sub *nats.Subscription
1357
1358 // Check for invalid values
1359 invalid := func() error {
1360 if err := sub.SetPendingLimits(0, 1); err == nil {
1361 return errors.New("Setting limit with 0 should fail")
1362 }
1363 if err := sub.SetPendingLimits(1, 0); err == nil {
1364 return errors.New("Setting limit with 0 should fail")
1365 }
1366 return nil
1367 }
1368 // function to send messages
1369 send := func(subject string, count int) {
1370 for range count {
1371 if err := nc.Publish(subject, payload); err != nil {
1372 t.Fatalf("Unexpected error on publish: %v", err)
1373 }
1374 }
1375 nc.Flush()
1376 }
1377
1378 // Check pending vs expected values
1379 var limitCount, limitBytes int
1380 var expectedCount, expectedBytes int
1381 checkPending := func() error {
1382 lc, lb, err := sub.PendingLimits()
1383 if err != nil {
1384 return err
1385 }
1386 if lc != limitCount || lb != limitBytes {
1387 return fmt.Errorf("Unexpected limits, expected %v msgs %v bytes, got %v msgs %v bytes",
1388 limitCount, limitBytes, lc, lb)
1389 }
1390 msgs, bytes, err := sub.Pending()
1391 if err != nil {
1392 return fmt.Errorf("Unexpected error getting pending counts: %v", err)
1393 }
1394 if (msgs != expectedCount && msgs != expectedCount-1) ||
1395 (bytes != expectedBytes && bytes != expectedBytes-payloadLen) {
1396 return fmt.Errorf("Unexpected counts, expected %v msgs %v bytes, got %v msgs %v bytes",
1397 expectedCount, expectedBytes, msgs, bytes)
1398 }
1399 return nil

Callers

nothing calls this directly

Calls 15

SetPendingLimitsMethod · 0.95
PendingLimitsMethod · 0.95
PendingMethod · 0.95
UnsubscribeMethod · 0.95
SetErrorHandlerMethod · 0.80
FatalfMethod · 0.80
ErrorfMethod · 0.80
RunDefaultServerFunction · 0.70
NewDefaultConnectionFunction · 0.70
WaitFunction · 0.70
PublishMethod · 0.65
SubscribeMethod · 0.65

Tested by

no test coverage detected