Debian Codegraph @

test/fanout_test.go

raw
1 // Copyright 2018-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //go:build !race && !skipnoracetests
15
16 package test
17
18 import (
19 "fmt"
20 "strconv"
21 "sync"
22 "testing"
23
24 "github.com/nats-io/nats-server/v2/server"
25 "github.com/nats-io/nats.go"
26 )
27
28 // IMPORTANT: Tests in this file are not executed when running with the -race flag.
29 // The test name should be prefixed with TestNoRace so we can run only
30 // those tests: go test -run=TestNoRace ...
31
32 // As we look to improve high fanout situations make sure we
33 // have a test that checks ordering for all subscriptions from a single subscriber.
34 func TestNoRaceHighFanoutOrdering(t *testing.T) {
35 opts := &server.Options{Host: "127.0.0.1", Port: server.RANDOM_PORT}
36
37 s := RunServer(opts)
38 defer s.Shutdown()
39
40 url := fmt.Sprintf("nats://%s", s.Addr())
41
42 const (
43 nconns = 100
44 nsubs = 100
45 npubs = 500
46 )
47
48 // make unique
49 subj := nats.NewInbox()
50
51 var wg sync.WaitGroup
52 wg.Add(nconns * nsubs)
53
54 for i := 0; i < nconns; i++ {
55 nc, err := nats.Connect(url)
56 if err != nil {
57 t.Fatalf("Expected a successful connect on %d, got %v\n", i, err)
58 }
59
60 nc.SetErrorHandler(func(c *nats.Conn, s *nats.Subscription, e error) {
61 t.Fatalf("Got an error %v for %+v\n", s, err)
62 })
63
64 for y := 0; y < nsubs; y++ {
65 expected := 0
66 nc.Subscribe(subj, func(msg *nats.Msg) {
67 n, _ := strconv.Atoi(string(msg.Data))
68 if n != expected {
69 t.Fatalf("Expected %d but received %d\n", expected, n)
70 }
71 expected++
72 if expected >= npubs {
73 wg.Done()
74 }
75 })
76 }
77 nc.Flush()
78 defer nc.Close()
79 }
80
81 nc, _ := nats.Connect(url)
82
83 for i := 0; i < npubs; i++ {
84 nc.Publish(subj, []byte(strconv.Itoa(i)))
85 }
86 defer nc.Close()
87
88 wg.Wait()
89 }
90
91 func TestNoRaceRouteFormTimeWithHighSubscriptions(t *testing.T) {
92 srvA, optsA := RunServerWithConfig("./configs/srv_a.conf")
93 defer srvA.Shutdown()
94
95 clientA := createClientConn(t, optsA.Host, optsA.Port)
96 defer clientA.Close()
97
98 sendA, expectA := setupConn(t, clientA)
99
100 // Now add lots of subscriptions. These will need to be forwarded
101 // to new routes when they are added.
102 subsTotal := 100000
103 for i := 0; i < subsTotal; i++ {
104 subject := fmt.Sprintf("FOO.BAR.BAZ.%d", i)
105 sendA(fmt.Sprintf("SUB %s %d\r\n", subject, i))
106 }
107 sendA("PING\r\n")
108 expectA(pongRe)
109
110 srvB, _ := RunServerWithConfig("./configs/srv_b.conf")
111 defer srvB.Shutdown()
112
113 checkClusterFormed(t, srvA, srvB)
114
115 // Now wait for all subscriptions to be processed.
116 if err := checkExpectedSubs(subsTotal, srvB); err != nil {
117 // Make sure we are not a slow consumer
118 // Check for slow consumer status
119 if srvA.NumSlowConsumers() > 0 {
120 t.Fatal("Did not receive all subscriptions due to slow consumer")
121 } else {
122 t.Fatalf("%v", err)
123 }
124 }
125 // Just double check the slow consumer status.
126 if srvA.NumSlowConsumers() > 0 {
127 t.Fatalf("Received a slow consumer notification: %d", srvA.NumSlowConsumers())
128 }
129 }