forked from tikv/client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheInvalidCallBackTest.java
More file actions
130 lines (116 loc) · 4.42 KB
/
CacheInvalidCallBackTest.java
File metadata and controls
130 lines (116 loc) · 4.42 KB
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
/*
* Copyright 2022 TiKV Project Authors.
*
* Licensed 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.tikv.common;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.junit.Test;
import org.tikv.common.event.CacheInvalidateEvent;
import org.tikv.common.region.RegionManager;
import org.tikv.common.region.RegionStoreClient;
import org.tikv.common.region.RegionStoreClient.RegionStoreClientBuilder;
import org.tikv.common.region.TiStore;
import org.tikv.common.util.BackOffer;
import org.tikv.common.util.ConcreteBackOffer;
import org.tikv.kvproto.Errorpb;
import org.tikv.kvproto.Errorpb.EpochNotMatch;
import org.tikv.kvproto.Errorpb.NotLeader;
import org.tikv.kvproto.Errorpb.StoreNotMatch;
import org.tikv.kvproto.Metapb;
public class CacheInvalidCallBackTest extends MockServerTest {
private RegionStoreClient createClient(
String version, Function<CacheInvalidateEvent, Void> cacheInvalidateCallBack) {
Metapb.Store meta =
Metapb.Store.newBuilder()
.setAddress(LOCAL_ADDR + ":" + port)
.setId(1)
.setState(Metapb.StoreState.Up)
.setVersion(version)
.build();
TiStore store = new TiStore(meta);
RegionManager manager = new RegionManager(session.getConf(), session.getPDClient());
manager.addCacheInvalidateCallback(cacheInvalidateCallBack);
RegionStoreClientBuilder builder =
new RegionStoreClientBuilder(
session.getConf(), session.getChannelFactory(), manager, session.getPDClient());
return builder.build(region, store);
}
@Test
public void testcacheInvalidCallBack() {
String version = "3.0.12";
CacheInvalidateCallBack cacheInvalidateCallBack = new CacheInvalidateCallBack();
doRawGetTest(createClient(version, cacheInvalidateCallBack), cacheInvalidateCallBack);
}
public void doRawGetTest(
RegionStoreClient client, CacheInvalidateCallBack cacheInvalidateCallBack) {
server.put("key1", "value1");
Optional<ByteString> value = client.rawGet(defaultBackOff(), ByteString.copyFromUtf8("key1"));
assertEquals(ByteString.copyFromUtf8("value1"), value.get());
try {
server.putError(
"error1", () -> Errorpb.Error.newBuilder().setNotLeader(NotLeader.getDefaultInstance()));
client.rawGet(defaultBackOff(), ByteString.copyFromUtf8("error1"));
fail();
} catch (Exception e) {
assertEquals(1, cacheInvalidateCallBack.cacheInvalidateEvents.size());
}
server.putError(
"failure",
() -> Errorpb.Error.newBuilder().setEpochNotMatch(EpochNotMatch.getDefaultInstance()));
try {
client.rawGet(defaultBackOff(), ByteString.copyFromUtf8("failure"));
fail();
} catch (Exception e) {
sleep(1000);
assertEquals(2, cacheInvalidateCallBack.cacheInvalidateEvents.size());
}
server.putError(
"store_not_match",
() -> Errorpb.Error.newBuilder().setStoreNotMatch(StoreNotMatch.getDefaultInstance()));
try {
client.rawGet(defaultBackOff(), ByteString.copyFromUtf8("failure"));
fail();
} catch (Exception e) {
sleep(1000);
assertEquals(3, cacheInvalidateCallBack.cacheInvalidateEvents.size());
}
server.clearAllMap();
client.close();
}
private void sleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
fail();
}
}
private BackOffer defaultBackOff() {
return ConcreteBackOffer.newCustomBackOff(1000);
}
static class CacheInvalidateCallBack implements Function<CacheInvalidateEvent, Void> {
public List<CacheInvalidateEvent> cacheInvalidateEvents = new ArrayList<>();
@Override
public Void apply(CacheInvalidateEvent cacheInvalidateEvent) {
cacheInvalidateEvents.add(cacheInvalidateEvent);
return null;
}
}
}