forked from apache/curator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestGroupMember.java
111 lines (98 loc) · 4.28 KB
/
TestGroupMember.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
/**
* 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.curator.framework.recipes.nodes;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.BaseClassForTests;
import org.apache.curator.test.Timing;
import org.apache.curator.test.compatibility.CuratorTestBase;
import org.apache.curator.utils.CloseableUtils;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.util.Map;
@Tag(CuratorTestBase.zk35TestCompatibilityGroup)
public class TestGroupMember extends BaseClassForTests
{
// NOTE - don't need many tests as this class is just a wrapper around two existing recipes
@Test
public void testBasic() throws Exception
{
Timing timing = new Timing();
GroupMember groupMember1 = null;
GroupMember groupMember2 = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
try
{
client.start();
groupMember1 = new GroupMember(client, "/member", "1");
assertTrue(groupMember1.getCurrentMembers().containsKey("1"));
groupMember1.start();
groupMember2 = new GroupMember(client, "/member", "2");
groupMember2.start();
timing.sleepABit();
Map<String, byte[]> currentMembers1 = groupMember1.getCurrentMembers();
Map<String, byte[]> currentMembers2 = groupMember2.getCurrentMembers();
Map<String, String> convertMembers1 = Maps.transformValues(currentMembers1, new Function<byte[], String>()
{
@Override
public String apply(byte[] input)
{
return new String(input);
}
});
Map<String, String> convertMembers2 = Maps.transformValues(currentMembers2, new Function<byte[], String>()
{
@Override
public String apply(byte[] input)
{
return new String(input);
}
});
assertEquals(convertMembers1.size(), 2);
assertEquals(convertMembers2.size(), 2);
assertEquals(convertMembers1, convertMembers2);
assertTrue(convertMembers1.containsKey("1"));
assertTrue(convertMembers1.containsKey("2"));
groupMember2.close();
timing.sleepABit();
currentMembers1 = groupMember1.getCurrentMembers();
assertEquals(currentMembers1.size(), 1);
assertTrue(currentMembers1.containsKey("1"));
assertFalse(currentMembers1.containsKey("2"));
groupMember1.setThisData("something".getBytes());
timing.sleepABit();
currentMembers1 = groupMember1.getCurrentMembers();
assertTrue(currentMembers1.containsKey("1"));
assertArrayEquals(currentMembers1.get("1"), "something".getBytes());
}
finally
{
CloseableUtils.closeQuietly(groupMember1);
CloseableUtils.closeQuietly(groupMember2);
CloseableUtils.closeQuietly(client);
}
}
}