get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/test/fanout_test.go (about) 1 // Copyright 2018-2019 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 // +build !race,!skipnoracetests 16 17 package test 18 19 import ( 20 "fmt" 21 "sync" 22 "testing" 23 24 "get.pme.sh/pnats/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 ec, _ := nats.NewEncodedConn(nc, nats.DEFAULT_ENCODER) 65 66 for y := 0; y < nsubs; y++ { 67 expected := 0 68 ec.Subscribe(subj, func(n int) { 69 if n != expected { 70 t.Fatalf("Expected %d but received %d\n", expected, n) 71 } 72 expected++ 73 if expected >= npubs { 74 wg.Done() 75 } 76 }) 77 } 78 ec.Flush() 79 defer ec.Close() 80 } 81 82 nc, _ := nats.Connect(url) 83 ec, _ := nats.NewEncodedConn(nc, nats.DEFAULT_ENCODER) 84 85 for i := 0; i < npubs; i++ { 86 ec.Publish(subj, i) 87 } 88 defer ec.Close() 89 90 wg.Wait() 91 } 92 93 func TestNoRaceRouteFormTimeWithHighSubscriptions(t *testing.T) { 94 srvA, optsA := RunServerWithConfig("./configs/srv_a.conf") 95 defer srvA.Shutdown() 96 97 clientA := createClientConn(t, optsA.Host, optsA.Port) 98 defer clientA.Close() 99 100 sendA, expectA := setupConn(t, clientA) 101 102 // Now add lots of subscriptions. These will need to be forwarded 103 // to new routes when they are added. 104 subsTotal := 100000 105 for i := 0; i < subsTotal; i++ { 106 subject := fmt.Sprintf("FOO.BAR.BAZ.%d", i) 107 sendA(fmt.Sprintf("SUB %s %d\r\n", subject, i)) 108 } 109 sendA("PING\r\n") 110 expectA(pongRe) 111 112 srvB, _ := RunServerWithConfig("./configs/srv_b.conf") 113 defer srvB.Shutdown() 114 115 checkClusterFormed(t, srvA, srvB) 116 117 // Now wait for all subscriptions to be processed. 118 if err := checkExpectedSubs(subsTotal, srvB); err != nil { 119 // Make sure we are not a slow consumer 120 // Check for slow consumer status 121 if srvA.NumSlowConsumers() > 0 { 122 t.Fatal("Did not receive all subscriptions due to slow consumer") 123 } else { 124 t.Fatalf("%v", err) 125 } 126 } 127 // Just double check the slow consumer status. 128 if srvA.NumSlowConsumers() > 0 { 129 t.Fatalf("Received a slow consumer notification: %d", srvA.NumSlowConsumers()) 130 } 131 }