This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathiris_test.go
119 lines (107 loc) · 3.37 KB
/
iris_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2013 Project Iris. All rights reserved.
//
// The current language binding is an official support library of the Iris
// cloud messaging framework, and as such, the same licensing terms apply.
// For details please see http://iris.karalabe.com/downloads#License
package iris
import (
"fmt"
"testing"
)
// Tests multiple concurrent client connections.
func TestConnect(t *testing.T) {
// Test specific configurations
conf := struct {
clients int
}{100}
// Start a batch of parallel connections
barrier := newBarrier(conf.clients)
for i := 0; i < conf.clients; i++ {
go func() {
// Connect to the local relay
conn, err := Connect(config.relay)
if err != nil {
barrier.Exit(fmt.Errorf("connection failed: %v", err))
return
}
barrier.Sync()
// Disconnect from the local relay
if err := conn.Close(); err != nil {
barrier.Exit(fmt.Errorf("connection close failed: %v", err))
return
}
barrier.Exit(nil)
}()
}
// Schedule the parallel operations
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("connection phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("termination phase failed: %v.", errs)
}
}
// Service handler for the registration tests.
type registerTestHandler struct{}
func (r *registerTestHandler) Init(conn *Connection) error { return nil }
func (r *registerTestHandler) HandleBroadcast(msg []byte) { panic("not implemented") }
func (r *registerTestHandler) HandleRequest(req []byte) ([]byte, error) { panic("not implemented") }
func (r *registerTestHandler) HandleTunnel(tun *Tunnel) { panic("not implemented") }
func (r *registerTestHandler) HandleDrop(reason error) { panic("not implemented") }
// Tests multiple concurrent service registrations.
func TestRegister(t *testing.T) {
// Test specific configurations
conf := struct {
services int
}{100}
// Start a batch of parallel connections
barrier := newBarrier(conf.services)
for i := 0; i < conf.services; i++ {
go func() {
// Register a new service to the relay
serv, err := Register(config.relay, config.cluster, new(registerTestHandler), nil)
if err != nil {
barrier.Exit(fmt.Errorf("registration failed: %v", err))
return
}
barrier.Sync()
// Unregister the service
if err := serv.Unregister(); err != nil {
barrier.Exit(fmt.Errorf("unregistration failed: %v", err))
return
}
barrier.Exit(nil)
}()
}
// Schedule the parallel operations
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("registration phase failed: %v.", errs)
}
if errs := barrier.Wait(); len(errs) != 0 {
t.Fatalf("unregistration phase failed: %v.", errs)
}
}
// Benchmarks client connection.
func BenchmarkConnect(b *testing.B) {
for i := 0; i < b.N; i++ {
if conn, err := Connect(config.relay); err != nil {
b.Fatalf("iteration %d: connection failed: %v.", i, err)
} else {
defer conn.Close()
}
}
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}
// Benchmarks service registration.
func BenchmarkRegister(b *testing.B) {
for i := 0; i < b.N; i++ {
if serv, err := Register(config.relay, config.cluster, new(registerTestHandler), nil); err != nil {
b.Fatalf("iteration %d: register failed: %v.", i, err)
} else {
defer serv.Unregister()
}
}
// Stop the timer (don't measure deferred cleanup)
b.StopTimer()
}