-
Notifications
You must be signed in to change notification settings - Fork 8
/
init_internal_test.go
143 lines (128 loc) · 4.24 KB
/
init_internal_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package v23
import (
"strings"
"testing"
"time"
"v.io/v23/context"
"v.io/v23/discovery"
"v.io/v23/flow"
"v.io/v23/namespace"
"v.io/v23/rpc"
"v.io/v23/security"
)
// Create a mock RuntimeFactory.
func MockFactory(ctx *context.T) (Runtime, *context.T, Shutdown, error) {
return &mockRuntime{}, nil, func() {}, nil
}
type mockRuntime struct{}
func (*mockRuntime) Init(ctx *context.T) error { return nil }
func (*mockRuntime) WithPrincipal(ctx *context.T, principal security.Principal) (*context.T, error) {
return nil, nil
}
func (*mockRuntime) GetPrincipal(ctx *context.T) security.Principal { return nil }
func (*mockRuntime) WithNewClient(ctx *context.T, opts ...rpc.ClientOpt) (*context.T, rpc.Client, error) {
return nil, nil, nil
}
func (*mockRuntime) GetClient(ctx *context.T) rpc.Client { return nil }
func (*mockRuntime) WithNewNamespace(ctx *context.T, roots ...string) (*context.T, namespace.T, error) {
return nil, nil, nil
}
func (*mockRuntime) GetNamespace(ctx *context.T) namespace.T { return nil }
func (*mockRuntime) GetAppCycle(ctx *context.T) AppCycle { return nil }
func (*mockRuntime) GetListenSpec(ctx *context.T) rpc.ListenSpec { return rpc.ListenSpec{} }
func (*mockRuntime) WithBackgroundContext(ctx *context.T) *context.T { return nil }
func (*mockRuntime) GetBackgroundContext(ctx *context.T) *context.T { return nil }
func (*mockRuntime) NewDiscovery(ctx *context.T) (discovery.T, error) { return nil, nil }
func (*mockRuntime) WithReservedNameDispatcher(ctx *context.T, d rpc.Dispatcher) *context.T {
return nil
}
func (*mockRuntime) GetReservedNameDispatcher(ctx *context.T) rpc.Dispatcher { return nil }
func (*mockRuntime) WithListenSpec(ctx *context.T, _ rpc.ListenSpec) *context.T { return ctx }
func (*mockRuntime) NewFlowManager(ctx *context.T, channelTimeout time.Duration) (flow.Manager, error) {
return nil, nil
}
func (*mockRuntime) WithNewServer(ctx *context.T, name string, object interface{}, auth security.Authorizer, opts ...rpc.ServerOpt) (*context.T, rpc.Server, error) {
return nil, nil, nil
}
func (*mockRuntime) WithNewDispatchingServer(ctx *context.T, name string, disp rpc.Dispatcher, opts ...rpc.ServerOpt) (*context.T, rpc.Server, error) {
return nil, nil, nil
}
func TestPanicOnInitWithNoRuntimeFactory(t *testing.T) {
clear()
// Calling Init without a registered RuntimeFactory should panic.
catcher := func() {
r := recover()
if r == nil {
t.Fatalf("recover returned nil")
}
err := r.(error)
str := err.Error()
if !strings.Contains(str, "No RuntimeFactory has been registered") {
t.Fatalf("unexpected error: %s", str)
}
}
defer catcher()
Init()
}
func TestInitWithRegisteredRuntimeFactory(t *testing.T) {
clear()
// Calling v23.Init with a RuntimeFactory should succeed.
RegisterRuntimeFactory(MockFactory)
Init()
}
func TestPanicOnSecondRuntimeFactoryRegistration(t *testing.T) {
clear()
// Registering multiple RuntimeFactory should fail.
RegisterRuntimeFactory(MockFactory)
catcher := func() {
r := recover()
if r == nil {
t.Fatalf("recover returned nil")
}
str := r.(string)
if !strings.Contains(str, "A RuntimeFactory has already been registered") {
t.Fatalf("unexpected error: %s", str)
}
}
defer catcher()
RegisterRuntimeFactory(MockFactory)
}
func TestPanicOnSecondInit(t *testing.T) {
clear()
// Calling Init again before calling shutdown should fail.
RegisterRuntimeFactory(MockFactory)
Init()
catcher := func() {
r := recover()
if r == nil {
t.Fatalf("recover returned nil")
}
err := r.(error)
str := err.Error()
if !strings.Contains(str, "A runtime has already been initialized") {
t.Fatalf("unexpected error: %s", str)
}
}
defer catcher()
Init()
}
func TestSecondInitAfterShutdown(t *testing.T) {
clear()
// Calling Init, shutting down, and then calling Init again should succeed.
RegisterRuntimeFactory(MockFactory)
_, shutdown := Init()
shutdown()
Init()
}
// for tests only
func clear() {
initState.mu.Lock()
initState.runtime = nil
initState.runtimeStack = ""
initState.runtimeFactory = nil
initState.runtimeFactoryStack = ""
initState.mu.Unlock()
}