-
Notifications
You must be signed in to change notification settings - Fork 939
/
Copy pathmock_registry.go
179 lines (154 loc) · 3.93 KB
/
mock_registry.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package registry
import (
"fmt"
"time"
)
import (
"github.com/dubbogo/gost/log/logger"
"go.uber.org/atomic"
)
import (
"dubbo.apache.org/dubbo-go/v3/common"
)
// MockRegistry is used as mock registry
type MockRegistry struct {
listener *listener
destroyed *atomic.Bool
allAddress chan []*ServiceEvent
}
// NewMockRegistry creates a mock registry
func NewMockRegistry(url *common.URL) (Registry, error) {
registry := &MockRegistry{
destroyed: atomic.NewBool(false),
allAddress: make(chan []*ServiceEvent),
}
listener := &listener{count: 0, registry: registry, listenChan: make(chan *ServiceEvent)}
registry.listener = listener
return registry, nil
}
// Register is used as a mock registry
func (*MockRegistry) Register(url *common.URL) error {
return nil
}
// nolint
func (r *MockRegistry) UnRegister(conf *common.URL) error {
return nil
}
// nolint
func (r *MockRegistry) Destroy() {
if r.destroyed.CAS(false, true) {
}
}
// IsAvailable is use for determine a mock registry available
func (r *MockRegistry) IsAvailable() bool {
return !r.destroyed.Load()
}
// nolint
func (r *MockRegistry) GetURL() *common.URL {
return nil
}
func (r *MockRegistry) subscribe(*common.URL) (Listener, error) {
return r.listener, nil
}
// nolint
func (r *MockRegistry) Subscribe(url *common.URL, notifyListener NotifyListener) error {
go func() {
for {
t, listener := r.checkLoopSubscribe(url)
if t == 0 {
continue
} else if t == -1 {
return
}
for {
serviceEvent, err := listener.Next()
if err != nil {
listener.Close()
time.Sleep(time.Duration(3) * time.Second)
return
}
logger.Infof("[Mock Registry] update begin, service event: %v", serviceEvent.String())
notifyListener.Notify(serviceEvent)
}
}
}()
go func() {
for {
t, _ := r.checkLoopSubscribe(url)
if t == 0 {
continue
} else if t == -1 {
return
}
for {
select {
case e := <-r.allAddress:
notifyListener.NotifyAll(e, func() {
fmt.Print("notify all ok")
})
break
}
}
}
}()
return nil
}
// UnSubscribe :
func (r *MockRegistry) UnSubscribe(url *common.URL, notifyListener NotifyListener) error {
return nil
}
// LoadSubscribeInstances load subscribe instance
func (r *MockRegistry) LoadSubscribeInstances(_ *common.URL, _ NotifyListener) error {
return nil
}
type listener struct {
count int64
registry *MockRegistry
listenChan chan *ServiceEvent
}
func (l *listener) Next() (*ServiceEvent, error) {
return <-l.listenChan, nil
}
func (*listener) Close() {
}
// nolint
func (r *MockRegistry) MockEvent(event *ServiceEvent) {
r.listener.listenChan <- event
}
// nolint
func (r *MockRegistry) MockEvents(events []*ServiceEvent) {
r.allAddress <- events
}
func (r *MockRegistry) checkLoopSubscribe(url *common.URL) (int, Listener) {
if !r.IsAvailable() {
logger.Warnf("event listener game over.")
time.Sleep(time.Duration(3) * time.Second)
return -1, nil
}
listener, err := r.subscribe(url)
if err != nil {
if !r.IsAvailable() {
logger.Warnf("event listener game over.")
return -1, nil
}
time.Sleep(time.Duration(3) * time.Second)
return 0, nil
}
return 1, listener
}