Debian Codegraph @

go/pkg/electron/common_test.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package electron
21
22 import (
23 "net"
24 "sync"
25 "testing"
26
27 "github.com/apache/qpid-proton/go/pkg/internal/test"
28 )
29
30 // AMQP client/server pair
31 type pair struct {
32 t testing.TB
33 client Session
34 server Connection
35 capacity int
36 prefetch bool
37 rchan chan Receiver
38 schan chan Sender
39 auth connectionSettings
40 }
41
42 func newPair(t testing.TB, cli, srv Connection) *pair {
43 cs, _ := cli.Session()
44 p := &pair{
45 t: t,
46 client: cs,
47 server: srv,
48 capacity: 100,
49 rchan: make(chan Receiver),
50 schan: make(chan Sender)}
51
52 go func() {
53 for i := range p.server.Incoming() {
54 switch i := i.(type) {
55 case *IncomingReceiver:
56 if p.capacity > 0 {
57 i.SetCapacity(p.capacity)
58 }
59 i.SetPrefetch(p.prefetch)
60 p.rchan <- i.Accept().(Receiver)
61 break
62 case *IncomingSender:
63 p.schan <- i.Accept().(Sender)
64 default:
65 i.Accept()
66 }
67 }
68 }()
69
70 return p
71 }
72
73 // AMQP pair linked by in-memory pipe
74 func newPipe(t testing.TB, clientOpts, serverOpts []ConnectionOption) *pair {
75 cli, srv := net.Pipe()
76 opts := []ConnectionOption{Server(), ContainerId("server")}
77 sc, _ := NewConnection(srv, append(opts, serverOpts...)...)
78 opts = []ConnectionOption{ContainerId("client")}
79 cc, _ := NewConnection(cli, append(opts, clientOpts...)...)
80 return newPair(t, cc, sc)
81 }
82
83 // AMQP pair linked by TCP socket
84 func newSocketPair(t testing.TB, clientOpts, serverOpts []ConnectionOption) *pair {
85 l, err := net.Listen("tcp4", ":0") // For systems with ipv6 disabled
86 test.FatalIfN(1, t, err)
87 var srv Connection
88 var srvErr error
89 var wg sync.WaitGroup
90 wg.Add(1)
91 go func() {
92 defer wg.Done()
93 srv, srvErr = NewContainer("server").Accept(l, serverOpts...)
94 }()
95 addr := l.Addr()
96 cli, err := NewContainer("client").Dial(addr.Network(), addr.String(), clientOpts...)
97 test.FatalIfN(1, t, err)
98 wg.Wait()
99 test.FatalIfN(1, t, srvErr)
100 return newPair(t, cli, srv)
101 }
102
103 func (p *pair) close() { p.client.Connection().Close(nil); p.server.Close(nil) }
104
105 // Return a client sender and server receiver
106 func (p *pair) sender(opts ...LinkOption) (Sender, Receiver) {
107 snd, err := p.client.Sender(opts...)
108 test.FatalIfN(1, p.t, err)
109 rcv := <-p.rchan
110 return snd, rcv
111 }
112
113 // Return a client receiver and server sender
114 func (p *pair) receiver(opts ...LinkOption) (Receiver, Sender) {
115 rcv, err := p.client.Receiver(opts...)
116 test.FatalIfN(1, p.t, err)
117 snd := <-p.schan
118 return rcv, snd
119 }