-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathConnectionsMapTest.java
147 lines (120 loc) · 6.17 KB
/
ConnectionsMapTest.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
package io.pinecone.clients;
import io.pinecone.configs.PineconeConfig;
import io.pinecone.configs.PineconeConnection;
import io.pinecone.exceptions.PineconeNotFoundException;
import io.pinecone.helpers.RandomStringBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openapitools.db_control.client.model.DeletionProtection;
import org.openapitools.db_control.client.model.IndexModel;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static io.pinecone.helpers.TestUtilities.waitUntilIndexIsReady;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ConnectionsMapTest {
static String indexName1;
static String indexName2;
static Pinecone pinecone1;
static Pinecone pinecone2;
@BeforeAll
public static void setUp() throws InterruptedException {
indexName1 = RandomStringBuilder.build("conn-map-index1", 5);
indexName2 = RandomStringBuilder.build("conn-map-index2", 5);
pinecone1 = new Pinecone
.Builder(System.getenv("PINECONE_API_KEY"))
.withSourceTag("pinecone_test")
.build();
pinecone2 = new Pinecone
.Builder(System.getenv("PINECONE_API_KEY"))
.withSourceTag("pinecone_test")
.build();
}
@Test
public void testMultipleIndexesWithMultipleClients() throws InterruptedException {
Map<String, String> tags = new HashMap<>();
tags.put("env", "test");
// Create index-1
pinecone1.createServerlessIndex(indexName1,
null,
3,
"aws",
"us-east-1",
DeletionProtection.DISABLED,
tags);
// Wait for index to be ready
IndexModel indexModel1 = waitUntilIndexIsReady(pinecone1, indexName1);
// Get index1's host
String host1 = indexModel1.getHost();
// Create config1 for getting index connection and set the host
PineconeConfig config1 = new PineconeConfig(System.getenv("PINECONE_API_KEY"));
config1.setHost(host1);
// Create index-2
pinecone1.createServerlessIndex(indexName2,
null,
3,
"aws",
"us-east-1",
DeletionProtection.DISABLED,
tags);
// Wait for index to be ready
IndexModel indexModel2 = waitUntilIndexIsReady(pinecone1, indexName2);
// Get index2's host
String host2 = indexModel2.getHost();
// Create config2 for getting index connection and set the host
PineconeConfig config2 = new PineconeConfig(System.getenv("PINECONE_API_KEY"));
config1.setHost(host2);
// Establish grpc connection for index-1
Index index1_1 = pinecone1.getIndexConnection(indexName1);
// Get connections map
ConcurrentHashMap<String, PineconeConnection> connectionsMap1_1 = pinecone1.getConnectionsMap();
// Verify connectionsMap contains only one <indexName, connection> pair i.e. for index1 and its connection
assertEquals(pinecone1.getConnectionsMap().size(), 1);
// Verify the value for index1 by comparing its value with host1 in the connectionsMap
assertEquals(host1, connectionsMap1_1.get(indexName1).toString());
// Establish grpc connection for index-2
Index index1_2 = pinecone1.getIndexConnection(indexName2);
// Get connections map after establishing second connection
ConcurrentHashMap<String, PineconeConnection> connectionsMap1_2 = pinecone1.getConnectionsMap();
// Verify connectionsMap contains two <indexName, connection> pairs i.e. for index1 and index2
assertEquals(connectionsMap1_2.size(), 2);
// Verify the values by checking host for both indexName1 and indexName2
assertEquals(host1, connectionsMap1_2.get(indexName1).toString());
assertEquals(host2, connectionsMap1_2.get(indexName2).toString());
// Establishing connections with index1 and index2 using another pinecone client
pinecone2.getConnection(indexName1, config1);
ConcurrentHashMap<String, PineconeConnection> connectionsMap2_1 = pinecone1.getConnectionsMap();
// Verify the new connections map is pointing to the same reference
assert connectionsMap2_1 == connectionsMap1_2;
// Verify the size of connections map is still 2 since the connection for index2 was not closed
assertEquals(2, connectionsMap2_1.size());
// Verify the connection value for index1 is host1
assertEquals(host1, connectionsMap2_1.get(indexName1).toString());
pinecone2.getConnection(indexName2, config2);
ConcurrentHashMap<String, PineconeConnection> connectionsMap2_2 = pinecone1.getConnectionsMap();
// Verify the new connections map is pointing to the same reference
assert connectionsMap2_1 == connectionsMap2_2;
// Verify the size of connections map is still 2 since the connections are not closed
assertEquals(2, connectionsMap2_2.size());
// Verify the values by checking host for both indexName1 and indexName2
assertEquals(host1, connectionsMap2_2.get(indexName1).toString());
assertEquals(host2, connectionsMap2_2.get(indexName2).toString());
// Close the connections
index1_1.close();
index1_2.close();
// Verify the map size is now 0
assertEquals(connectionsMap1_1.size(), 0);
assertEquals(connectionsMap1_2.size(), 0);
assertEquals(connectionsMap2_1.size(), 0);
assertEquals(connectionsMap2_2.size(), 0);
// Delete the indexes
pinecone1.deleteIndex(indexName1);
pinecone1.deleteIndex(indexName2);
// Wait for indexes to be deleted
Thread.sleep(5000);
// Confirm the indexes are deleted by calling describe index which should return resource not found
assertThrows(PineconeNotFoundException.class, () -> pinecone1.describeIndex(indexName1));
assertThrows(PineconeNotFoundException.class, () -> pinecone1.describeIndex(indexName2));
}
}