github.com/gdamore/mangos@v1.4.0/test/expire_test.go (about) 1 // Copyright 2018 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 "bytes" 19 "runtime" 20 "testing" 21 "time" 22 23 "nanomsg.org/go-mangos" 24 "nanomsg.org/go-mangos/protocol/pair" 25 "nanomsg.org/go-mangos/transport/inproc" 26 ) 27 28 func TestExpireDrop(t *testing.T) { 29 inp := inproc.NewTransport() 30 addr := AddrTestInp + "EXPIRE" 31 32 if runtime.GOOS == "windows" { 33 t.Skip("Windows clock resolution too coarse") 34 return 35 } 36 srv, err := pair.NewSocket() 37 if err != nil { 38 t.Errorf("Failed to make server: %v", err) 39 return 40 } 41 defer srv.Close() 42 srv.AddTransport(inp) 43 44 err = srv.Listen(addr) 45 if err != nil { 46 t.Errorf("Failed listen: %v", err) 47 return 48 } 49 50 cli, err := pair.NewSocket() 51 if err != nil { 52 t.Errorf("Failed to make client: %v", err) 53 return 54 } 55 defer cli.Close() 56 57 cli.AddTransport(inp) 58 59 err = cli.Dial(addr) 60 if err != nil { 61 t.Errorf("Failed dial: %v", err) 62 return 63 } 64 65 time.Sleep(time.Millisecond * 100) 66 67 err = srv.SetOption(mangos.OptionRecvDeadline, time.Millisecond*10) 68 if err != nil { 69 t.Errorf("Failed set recv deadline") 70 return 71 } 72 73 if err = cli.Send([]byte("GOOD")); err != nil { 74 t.Errorf("Failed first send: %v", err) 75 return 76 } 77 // Now try setting the option. We set it to a very short window. 78 // Note that we default to have a non-zero queue depth, so we will 79 // still queue the message, but then when we retrieve the message 80 // it will be found to be stale. 81 err = cli.SetOption(mangos.OptionSendDeadline, time.Nanosecond*1) 82 if err != nil { 83 t.Errorf("Failed set send deadline") 84 return 85 } 86 87 if err = cli.Send([]byte("DROP")); err != nil { 88 t.Errorf("Failed send drop: %v", err) 89 return 90 } 91 92 v, err := srv.Recv() 93 if err != nil { 94 t.Errorf("Failed first recv: %v", err) 95 return 96 } else if !bytes.Equal(v, []byte("GOOD")) { 97 t.Errorf("Got wrong message: %v", v) 98 return 99 } else { 100 t.Logf("Got good message: %v", v) 101 } 102 103 v, err = srv.Recv() 104 switch err { 105 case mangos.ErrRecvTimeout: // expected 106 t.Logf("Deadline honored") 107 case nil: 108 t.Errorf("Message not dropped: %v", v) 109 default: 110 t.Errorf("Got unexpected error: %v", err) 111 } 112 }