go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/pipeevent_test.go (about) 1 // Copyright 2019 The Mangos Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use file except in compliance with the License. 5 // You may obtain a copy of the license at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "sync" 19 "testing" 20 "time" 21 22 "go.nanomsg.org/mangos/v3" 23 "go.nanomsg.org/mangos/v3/protocol/pair" 24 _ "go.nanomsg.org/mangos/v3/transport/inproc" 25 ) 26 27 func TestPipeHook(t *testing.T) { 28 self := GetSocket(t, pair.NewSocket) 29 defer MustClose(t, self) 30 peer := GetSocket(t, pair.NewSocket) 31 32 type event struct { 33 cnt map[mangos.PipeEvent]int 34 p mangos.Pipe 35 } 36 37 var lock sync.Mutex 38 events := make(map[mangos.Pipe]event) 39 40 hook := func(ev mangos.PipeEvent, p mangos.Pipe) { 41 lock.Lock() 42 defer lock.Unlock() 43 if item, ok := events[p]; ok { 44 item.cnt[ev]++ 45 } else { 46 cnt := make(map[mangos.PipeEvent]int) 47 cnt[ev] = 1 48 events[p] = event{ 49 cnt: cnt, 50 p: p, 51 } 52 } 53 } 54 55 pass := 0 56 self.SetPipeEventHook(hook) 57 58 addr := AddrTestInp() 59 MustSucceed(t, peer.Listen(addr)) 60 MustSucceed(t, self.Dial(addr)) 61 62 lock.Lock() 63 for p, item := range events { 64 MustBeTrue(t, p == item.p) 65 MustBeTrue(t, item.cnt[mangos.PipeEventAttaching] == 1) 66 MustBeTrue(t, item.cnt[mangos.PipeEventAttached] == 1) 67 MustBeTrue(t, item.cnt[mangos.PipeEventDetached] == 0) 68 pass++ 69 } 70 MustBeTrue(t, pass == 1) 71 pass = 0 72 lock.Unlock() 73 74 MustClose(t, peer) 75 time.Sleep(time.Millisecond * 100) 76 77 lock.Lock() 78 for p, item := range events { 79 MustBeTrue(t, p == item.p) 80 MustBeTrue(t, item.cnt[mangos.PipeEventAttaching] == 1) 81 MustBeTrue(t, item.cnt[mangos.PipeEventAttached] == 1) 82 MustBeTrue(t, item.cnt[mangos.PipeEventDetached] == 1) 83 pass++ 84 } 85 MustBeTrue(t, pass == 1) 86 lock.Unlock() 87 } 88 89 func TestPipeHookReject(t *testing.T) { 90 self := GetSocket(t, pair.NewSocket) 91 defer MustClose(t, self) 92 peer := GetSocket(t, pair.NewSocket) 93 94 type event struct { 95 cnt map[mangos.PipeEvent]int 96 p mangos.Pipe 97 } 98 99 var lock sync.Mutex 100 events := make(map[mangos.Pipe]*event) 101 102 hook := func(ev mangos.PipeEvent, p mangos.Pipe) { 103 lock.Lock() 104 defer lock.Unlock() 105 if item, ok := events[p]; ok { 106 item.cnt[ev]++ 107 } else { 108 cnt := make(map[mangos.PipeEvent]int) 109 cnt[ev] = 1 110 events[p] = &event{ 111 cnt: cnt, 112 p: p, 113 } 114 } 115 if ev == mangos.PipeEventAttaching { 116 _ = p.Close() 117 } 118 } 119 120 MustSucceed(t, self.SetOption(mangos.OptionReconnectTime, time.Millisecond*10)) 121 MustSucceed(t, self.SetOption(mangos.OptionMaxReconnectTime, time.Millisecond*20)) 122 MustSucceed(t, self.SetOption(mangos.OptionDialAsynch, true)) 123 self.SetPipeEventHook(hook) 124 125 addr := AddrTestInp() 126 MustSucceed(t, peer.Listen(addr)) 127 MustSucceed(t, self.Dial(addr)) 128 129 time.Sleep(time.Millisecond * 100) 130 lock.Lock() 131 pass := 0 132 for p, item := range events { 133 MustBeTrue(t, p == item.p) 134 MustBeTrue(t, item.cnt[mangos.PipeEventAttaching] == 1) 135 MustBeTrue(t, item.cnt[mangos.PipeEventAttached] == 0) 136 MustBeTrue(t, item.cnt[mangos.PipeEventDetached] == 0) 137 pass++ 138 } 139 MustBeTrue(t, pass > 2) 140 lock.Unlock() 141 142 MustClose(t, peer) 143 time.Sleep(time.Millisecond * 100) 144 145 lock.Lock() 146 pass = 0 147 for p, item := range events { 148 MustBeTrue(t, p == item.p) 149 MustBeTrue(t, item.cnt[mangos.PipeEventAttaching] == 1) 150 MustBeTrue(t, item.cnt[mangos.PipeEventAttached] == 0) 151 MustBeTrue(t, item.cnt[mangos.PipeEventDetached] == 0) 152 pass++ 153 } 154 MustBeTrue(t, pass > 2) 155 lock.Unlock() 156 }