go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/core/pipe_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 core 16 17 import ( 18 "testing" 19 ) 20 21 // This is an application-wide global ID allocator. Unfortunately we need 22 // to have unique pipe IDs globally to permit certain things to work 23 // correctly. 24 25 func TestPipeAllocatorWrap(t *testing.T) { 26 p := &pipeIDAllocator{} 27 _ = p.Get() // as it will reset otherwise 28 p.next = 0 29 p.next-- 30 if v := p.Get(); v != 0x7fffffff { 31 t.Errorf("Got wrong value for wrap: %x", v) 32 } 33 // skip zero 34 if v := p.Get(); v != 1 { 35 t.Errorf("Got wrong value for wrap: %x", v) 36 } 37 } 38 39 func TestPipeAllocatorReuse(t *testing.T) { 40 p := &pipeIDAllocator{} 41 _ = p.Get() // as it will reset otherwise 42 p.next = 0 43 p.next-- 44 if v := p.Get(); v != 0x7fffffff { 45 t.Errorf("Got wrong value for wrap: %x", v) 46 } 47 // skip zero 48 if v := p.Get(); v != 1 { 49 t.Errorf("Got wrong value for wrap: %x", v) 50 } 51 p.next = 0 52 if v := p.Get(); v != 2 { 53 t.Errorf("Maybe reused value?: %x", v) 54 } 55 } 56 57 func TestPipeAllocatorRandom(t *testing.T) { 58 p1 := &pipeIDAllocator{} 59 p2 := &pipeIDAllocator{} 60 61 v1 := p1.Get() 62 v2 := p2.Get() 63 64 if v1 == v2 { 65 t.Errorf("values not random: %v %v", v1, v2) 66 } 67 } 68 69 func TestPipeAllocatorUnusedFree(t *testing.T) { 70 defer func() { 71 pass := false 72 if r := recover(); r != nil { 73 if r != "free of unused pipe ID" { 74 t.Errorf("Wrong value for r: %v", r) 75 } 76 pass = true 77 } 78 if !pass { 79 t.Error("Unused free did not panic") 80 } 81 }() 82 83 p := &pipeIDAllocator{} 84 p.Free(2) 85 } 86 87 func TestPipeNoAddress(t *testing.T) { 88 p := &pipe{} 89 if addr := p.Address(); addr != "" { 90 t.Errorf("Got unexpected address: %v", addr) 91 } 92 }