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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.host;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import com.yahoo.vespa.config.server.host.HostRegistry;
import org.junit.Test;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
/**
* @author lulf
* @since 5.3
*/
public class HostRegistryTest {
@Test
public void old_hosts_are_removed() {
HostRegistry<String> reg = new HostRegistry<>();
assertNull(reg.getKeyForHost("foo.com"));
reg.update("fookey", Arrays.asList("foo.com", "bar.com", "baz.com"));
assertGetKey(reg, "foo.com", "fookey");
assertGetKey(reg, "bar.com", "fookey");
assertGetKey(reg, "baz.com", "fookey");
assertThat(reg.getAllHosts().size(), is(3));
reg.update("fookey", Arrays.asList("bar.com", "baz.com"));
assertNull(reg.getKeyForHost("foo.com"));
assertGetKey(reg, "bar.com", "fookey");
assertGetKey(reg, "baz.com", "fookey");
assertThat(reg.getAllHosts().size(), is(2));
assertThat(reg.getAllHosts(), contains("bar.com", "baz.com"));
reg.removeHostsForKey("fookey");
assertThat(reg.getAllHosts().size(), is(0));
assertNull(reg.getKeyForHost("foo.com"));
assertNull(reg.getKeyForHost("bar.com"));
}
@Test
public void multiple_keys_are_handled() {
HostRegistry<String> reg = new HostRegistry<>();
reg.update("fookey", Arrays.asList("foo.com", "bar.com"));
reg.update("barkey", Arrays.asList("baz.com", "quux.com"));
assertGetKey(reg, "foo.com", "fookey");
assertGetKey(reg, "bar.com", "fookey");
assertGetKey(reg, "baz.com", "barkey");
assertGetKey(reg, "quux.com", "barkey");
}
@Test(expected = IllegalArgumentException.class)
public void keys_cannot_overlap() {
HostRegistry<String> reg = new HostRegistry<>();
reg.update("fookey", Arrays.asList("foo.com", "bar.com"));
reg.update("barkey", Arrays.asList("bar.com", "baz.com"));
}
@Test
public void all_hosts_are_returned() {
HostRegistry<String> reg = new HostRegistry<>();
reg.update("fookey", Arrays.asList("foo.com", "bar.com"));
reg.update("barkey", Arrays.asList("baz.com", "quux.com"));
assertThat(reg.getAllHosts().size(), is(4));
}
@Test
public void ensure_that_collection_is_copied() {
HostRegistry<String> reg = new HostRegistry<>();
List<String> hosts = new ArrayList<>(Arrays.asList("foo.com", "bar.com", "baz.com"));
reg.update("fookey", hosts);
assertThat(reg.getCurrentHosts("fookey").size(), is(3));
hosts.remove(2);
assertThat(reg.getCurrentHosts("fookey").size(), is(3));
}
@Test
public void ensure_that_underlying_hosts_do_not_change() {
HostRegistry<String> reg = new HostRegistry<>();
reg.update("fookey", new ArrayList<>(Arrays.asList("foo.com", "bar.com", "baz.com")));
Collection<String> hosts = reg.getAllHosts();
assertThat(hosts.size(), is(3));
reg.update("fookey", new ArrayList<>(Arrays.asList("foo.com")));
assertThat(hosts.size(), is(3));
}
private void assertGetKey(HostRegistry<String> reg, String host, String expectedKey) {
assertNotNull(reg.getKeyForHost(host));
assertThat(reg.getKeyForHost(host), is(expectedKey));
}
}
|