github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/pool/default_test.go (about) 1 package pool 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func testPool(t *testing.T, size int, ttl time.Duration) { 9 /* // mock transport 10 tr := transport.NewMemoryTransport() 11 12 options := Options{ 13 TTL: ttl, 14 Size: size, 15 Transport: tr, 16 } 17 // zero pool 18 p := newPool(options) 19 20 // listen 21 l, err := tr.Listen(":0") 22 if err != nil { 23 t.Fatal(err) 24 } 25 defer l.Close() 26 27 // accept loop 28 go func() { 29 for { 30 if err := l.Accept(func(s transport.Socket) { 31 for { 32 var msg transport.Message 33 if err := s.Recv(&msg); err != nil { 34 return 35 } 36 if err := s.Send(&msg); err != nil { 37 return 38 } 39 } 40 }); err != nil { 41 return 42 } 43 } 44 }() 45 46 for i := 0; i < 10; i++ { 47 // get a conn 48 c, err := p.Get(l.Addr()) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 msg := &transport.Message{ 54 Body: []byte(`hello world`), 55 } 56 57 if err := c.Send(msg); err != nil { 58 t.Fatal(err) 59 } 60 61 var rcv transport.Message 62 63 if err := c.Recv(&rcv); err != nil { 64 t.Fatal(err) 65 } 66 67 if string(rcv.Body) != string(msg.Body) { 68 t.Fatalf("got %v, expected %v", rcv.Body, msg.Body) 69 } 70 71 // release the conn 72 p.Release(c, nil) 73 74 p.Lock() 75 if i := len(p.conns[l.Addr()]); i > size { 76 p.Unlock() 77 t.Fatalf("pool size %d is greater than expected %d", i, size) 78 } 79 p.Unlock() 80 } 81 */ 82 } 83 84 func TestClientPool(t *testing.T) { 85 testPool(t, 0, time.Minute) 86 testPool(t, 2, time.Minute) 87 }