-
Notifications
You must be signed in to change notification settings - Fork 803
/
StoreServiceTest.java
149 lines (117 loc) · 5.99 KB
/
StoreServiceTest.java
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
/*
* 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 org.apache.servicecomb.registry.lightweight;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.Matchers.any;
import java.util.concurrent.CompletableFuture;
import org.apache.servicecomb.foundation.test.scaffolding.MockTicker;
import org.apache.servicecomb.registry.api.registry.Microservice;
import org.apache.servicecomb.registry.api.registry.MicroserviceInstanceStatus;
import org.apache.servicecomb.registry.lightweight.store.InstanceStore;
import org.apache.servicecomb.registry.lightweight.store.MicroserviceStore;
import org.apache.servicecomb.registry.lightweight.store.Store;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import io.vertx.core.json.Json;
class StoreServiceTest extends TestBase {
EventBus eventBus = new EventBus();
MockTicker ticker = new MockTicker();
Store store = new Store().setTicker(ticker);
DiscoveryClient discoveryClient = Mockito.mock(DiscoveryClient.class);
StoreService service = new StoreService(eventBus, store, discoveryClient);
@BeforeEach
void setUp() {
Mockito.when(discoveryClient.getInfoAsync(any(), any()))
.thenReturn(CompletableFuture.completedFuture(self.getMicroserviceInfo()));
Mockito.when(discoveryClient.getInstanceAsync(any(), any()))
.thenReturn(CompletableFuture.completedFuture(self.getInstance()));
}
@Test
void should_register_microservice_and_instance_when_both_not_exist() {
RegisterRequest request = self.buildRegisterRequest();
InstanceStore instanceStore = service.register(request);
assertThat(store.findMicroserviceStore(self.getServiceId()).getMicroservice())
.isSameAs(self.getMicroservice());
assertThat(instanceStore.getInstance()).isSameAs(self.getInstance());
assertThat(self.getInstance().getStatus()).isEqualTo(MicroserviceInstanceStatus.UP);
}
@Test
void should_register_instance_when_microservice_exist() {
Microservice microservice = Json.decodeValue(Json.encode(self.getMicroservice()), Microservice.class);
store.addMicroservice(microservice, self.getSchemasSummary());
RegisterRequest request = self.buildRegisterRequest();
InstanceStore instanceStore = service.register(request);
assertThat(microservice).isNotSameAs(self.getMicroservice());
assertThat(store.findMicroserviceStore(self.getServiceId()).getMicroservice())
.isSameAs(microservice);
assertThat(instanceStore.getInstance()).isSameAs(self.getInstance());
}
@Test
void should_reject_when_schema_changed_and_has_existing_instance() {
should_register_microservice_and_instance_when_both_not_exist();
RegisterRequest request = self.buildRegisterRequest()
.setInstanceId("new id")
.setSchemasSummary("new summary");
Throwable throwable = catchThrowable(() -> service.register(request));
assertThat(throwable)
.isInstanceOf(RegisterException.class)
.hasMessage("schemas changed, but version not changed, and has 1 existing instances");
}
SchemaChangedEvent schemaChangedEvent;
@Subscribe
public void onSchemaChanged(SchemaChangedEvent event) {
this.schemaChangedEvent = event;
}
@Test
void should_register_and_notify_when_schema_changed_and_has_not_existing_instance() {
should_register_microservice_and_instance_when_both_not_exist();
MicroserviceStore microserviceStore = store.findMicroserviceStore(self.getServiceId());
assertThat(microserviceStore.hasInstance()).isTrue();
store.deleteInstance(self.getServiceId(), self.getInstanceId());
assertThat(microserviceStore.hasInstance()).isFalse();
eventBus.register(this);
RegisterRequest request = self.buildRegisterRequest()
.setSchemasSummary("new summary");
InstanceStore instanceStore = service.register(request);
assertThat(microserviceStore.isSchemaChanged("new summary")).isFalse();
assertThat(instanceStore.getInstance()).isSameAs(self.getInstance());
assertThat(schemaChangedEvent.getMicroservice()).isSameAs(self.getMicroservice());
}
@Test
void should_allow_update_instance_status() {
should_register_microservice_and_instance_when_both_not_exist();
RegisterRequest request = self.buildRegisterRequest()
.setStatus(MicroserviceInstanceStatus.TESTING);
ticker.setValues(1);
InstanceStore instanceStore = service.register(request);
assertThat(self.getInstance().getStatus()).isEqualTo(MicroserviceInstanceStatus.TESTING);
assertThat(instanceStore.getLastHeartBeat()).isEqualTo(1);
}
@Test
void should_process_as_heartbeat_when_nothing_changed() {
should_register_microservice_and_instance_when_both_not_exist();
InstanceStore instanceStore = store.findInstanceStore(self.getInstanceId());
assertThat(instanceStore.getLastHeartBeat()).isEqualTo(0);
ticker.setValues(1);
should_register_microservice_and_instance_when_both_not_exist();
assertThat(instanceStore.getLastHeartBeat()).isEqualTo(1);
}
}