| 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 proton |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"net" |
| 25 |
"testing" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/apache/qpid-proton/go/pkg/internal/test" |
| 29 |
) |
| 30 |
|
| 31 |
type events []EventType |
| 32 |
|
| 33 |
type testEngine struct { |
| 34 |
Engine |
| 35 |
events chan EventType |
| 36 |
} |
| 37 |
|
| 38 |
func newTestEngine(conn net.Conn) (*testEngine, error) { |
| 39 |
testEng := &testEngine{events: make(chan EventType, 1000)} |
| 40 |
return testEng, testEng.Initialize(conn, testEng) |
| 41 |
} |
| 42 |
|
| 43 |
func (eng *testEngine) HandleEvent(e Event) { |
| 44 |
eng.events <- e.Type() |
| 45 |
} |
| 46 |
|
| 47 |
func (eng *testEngine) expect(events []EventType) error { |
| 48 |
timer := time.After(5 * time.Second) |
| 49 |
for _, want := range events { |
| 50 |
select { |
| 51 |
case got := <-eng.events: |
| 52 |
if want != got { |
| 53 |
return fmt.Errorf("want %s, got %s", want, got) |
| 54 |
} |
| 55 |
case <-timer: |
| 56 |
return fmt.Errorf("expect timeout") |
| 57 |
} |
| 58 |
} |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
func Test(t *testing.T) { |
| 63 |
cConn, sConn := net.Pipe() |
| 64 |
client, err := newTestEngine(cConn) |
| 65 |
test.FatalIf(t, err) |
| 66 |
server, err := newTestEngine(sConn) |
| 67 |
test.FatalIf(t, err) |
| 68 |
server.Server() |
| 69 |
go client.Run() |
| 70 |
go server.Run() |
| 71 |
test.FatalIf(t, server.expect(events{EConnectionInit, EConnectionBound})) |
| 72 |
test.FatalIf(t, client.expect(events{EConnectionInit, EConnectionBound})) |
| 73 |
test.FatalIf(t, client.InjectWait(func() error { client.Connection().Open(); return nil })) |
| 74 |
test.FatalIf(t, client.expect(events{EConnectionLocalOpen})) |
| 75 |
test.FatalIf(t, server.expect(events{EConnectionRemoteOpen})) |
| 76 |
} |