@@ -5,132 +5,37 @@ import NIO
55import TestsCommon
66import XCTest
77
8- final class ServerSelectionTests : MongoSwiftTestCase {
9- // Servers
10- let standaloneServer = ServerDescription ( type: . standalone)
11- let rsPrimaryServer = ServerDescription ( type: . rsPrimary)
12- let rsSecondaryServer1 = ServerDescription ( type: . rsSecondary, tags: [ " dc " : " ny " , " rack " : " 2 " , " size " : " large " ] )
13- let rsSecondaryServer2 = ServerDescription ( type: . rsSecondary)
14- let rsSecondaryServer3 = ServerDescription ( type: . rsSecondary, tags: [ " dc " : " ny " , " rack " : " 3 " , " size " : " small " ] )
15- let mongosServer = ServerDescription ( type: . mongos)
16-
17- // Read Preferences
18- let primaryReadPreference = ReadPreference ( . primary)
19- let primaryPrefReadPreferemce = ReadPreference ( . primaryPreferred)
20-
21- // Tag Sets
22- let tagSet : BSONDocument = [ " dc " : " ny " , " rack " : " 2 " ]
23- let tagSet2 : BSONDocument = [ " dc " : " ny " ]
24- let tagSet3 : BSONDocument = [ " size " : " small " ]
25-
26- func testUnknownTopology( ) {
27- let unkownTopology = TopologyDescription ( type: . unknown, servers: [ standaloneServer] )
28- expect ( unkownTopology. findSuitableServers ( ) ) . to ( haveCount ( 0 ) )
29- }
30-
31- func testSingleTopology( ) {
32- let singleTopology = TopologyDescription ( type: . single, servers: [ standaloneServer] )
33- expect ( singleTopology. findSuitableServers ( ) [ 0 ] . type) . to ( equal ( . standalone) )
34- }
35-
36- func testReplicaSetWithPrimaryTopology( ) {
37- let replicaSetTopology = TopologyDescription ( type: . replicaSetWithPrimary, servers: [
38- rsPrimaryServer,
39- rsSecondaryServer1,
40- rsSecondaryServer2
41- ] )
42- let replicaSetSuitableServers = replicaSetTopology
43- . findSuitableServers ( readPreference: self . primaryReadPreference)
44- expect ( replicaSetSuitableServers [ 0 ] . type) . to ( equal ( . rsPrimary) )
45- expect ( replicaSetSuitableServers) . to ( haveCount ( 1 ) )
46-
47- let replicaSetSuitableServers2 = replicaSetTopology
48- . findSuitableServers ( readPreference: self . primaryPrefReadPreferemce)
49- expect ( replicaSetSuitableServers2 [ 0 ] . type) . to ( equal ( . rsPrimary) )
50- expect ( replicaSetSuitableServers2) . to ( haveCount ( 1 ) )
51- }
52-
53- func testReplicaSetNoPrimaryTopology( ) {
54- let replicaSetNoPrimaryTopology = TopologyDescription ( type: . replicaSetNoPrimary, servers: [
55- rsSecondaryServer1,
56- rsSecondaryServer2
57- ] )
58- let suitable1 = replicaSetNoPrimaryTopology
59- . findSuitableServers ( readPreference: self . primaryReadPreference)
60- expect ( suitable1) . to ( haveCount ( 0 ) )
61-
62- let suitable2 = replicaSetNoPrimaryTopology
63- . findSuitableServers ( readPreference: nil )
64- expect ( suitable2) . to ( haveCount ( 0 ) )
65-
66- let suitable3 = replicaSetNoPrimaryTopology
67- . findSuitableServers ( readPreference: self . primaryPrefReadPreferemce)
68- expect ( suitable3 [ 0 ] . type) . to ( equal ( . rsSecondary) )
69- expect ( suitable3) . to ( haveCount ( 2 ) )
8+ private struct ServerSelectionLogicTestFile : Decodable {
9+ let topologyDescription : TopologyDescription
10+ let operation : OperationType
11+ let readPreference : ReadPreference
12+ let suitableServers : [ ServerDescription ]
13+ let inLatencyWindow : [ ServerDescription ]
14+
15+ enum CodingKeys : String , CodingKey {
16+ case topologyDescription = " topology_description " , operation, readPreference = " read_preference " ,
17+ suitableServers = " suitable_servers " , inLatencyWindow = " in_latency_window "
7018 }
19+ }
7120
72- func testShardedTopology( ) {
73- let shardedTopology = TopologyDescription ( type: . sharded, servers: [
74- mongosServer
75- ] )
76- let shardedSuitableServers = shardedTopology. findSuitableServers ( )
77- expect ( shardedSuitableServers [ 0 ] . type)
78- . to ( equal ( . mongos) )
79- expect ( shardedSuitableServers) . to ( haveCount ( 1 ) )
80- }
81-
82- func testTagSets( ) throws {
83- // tag set 1
84- let topology = TopologyDescription ( type: . replicaSetNoPrimary, servers: [
85- rsSecondaryServer1,
86- rsSecondaryServer2,
87- rsSecondaryServer3
88- ] )
89- let secondaryReadPreferenceWithTagSet = try ReadPreference (
90- . secondaryPreferred,
91- tagSets: [ tagSet, tagSet3] , // tagSet3 should be ignored, because tagSet matches some servers
92- maxStalenessSeconds: nil
93- )
94-
95- let suitable = topology. findSuitableServers ( readPreference: secondaryReadPreferenceWithTagSet)
96- expect ( suitable [ 0 ] . type) . to ( equal ( . rsSecondary) )
97- expect ( suitable) . to ( haveCount ( 1 ) )
98-
99- // tag set 2
100- let secondaryReadPreferenceWithTagSet2 = try ReadPreference (
101- . secondaryPreferred,
102- tagSets: [ tagSet2] ,
103- maxStalenessSeconds: nil
104- )
105-
106- let suitable2 = topology. findSuitableServers ( readPreference: secondaryReadPreferenceWithTagSet2)
107- expect ( suitable2 [ 0 ] . type) . to ( equal ( . rsSecondary) )
108- expect ( suitable2) . to ( haveCount ( 2 ) )
109-
110- // invalid tag set passing
111- expect ( try ReadPreference (
112- . primary,
113- tagSets: [ self . tagSet] ,
114- maxStalenessSeconds: nil
115- ) ) . to ( throwError ( errorType: MongoError . InvalidArgumentError. self) )
116-
117- // valid tag set passing
118- let replicaSetTopology = TopologyDescription ( type: . replicaSetWithPrimary, servers: [
119- rsPrimaryServer,
120- rsSecondaryServer1,
121- rsSecondaryServer2
122- ] )
123-
124- let emptyTagSet : BSONDocument = [ : ]
21+ private enum OperationType : String , Decodable {
22+ case read, write
23+ }
12524
126- let primaryReadPreferenceWithEmptyTagSet = try ReadPreference (
127- . primary,
128- tagSets: [ emptyTagSet] ,
129- maxStalenessSeconds: nil
25+ final class ServerSelectionTests : MongoSwiftTestCase {
26+ func testServerSelectionLogic( ) throws {
27+ let tests = try retrieveSpecTestFiles (
28+ specName: " server-selection " ,
29+ subdirectory: " server_selection " ,
30+ asType: ServerSelectionLogicTestFile . self
13031 )
131- let replicaSetSuitableServers = replicaSetTopology
132- . findSuitableServers ( readPreference: primaryReadPreferenceWithEmptyTagSet)
133- expect ( replicaSetSuitableServers [ 0 ] . type) . to ( equal ( . rsPrimary) )
134- expect ( replicaSetSuitableServers) . to ( haveCount ( 1 ) )
32+ for (filename, test) in tests {
33+ print ( " Running test from \( filename) ... " )
34+ // Server selection assumes that no read preference is passed for write operations.
35+ let readPreference = test. operation == . read ? test. readPreference : nil
36+ let selectedServers = test. topologyDescription. findSuitableServers ( readPreference: readPreference)
37+ expect ( selectedServers. count) . to ( equal ( test. suitableServers. count) )
38+ expect ( selectedServers) . to ( contain ( test. suitableServers) )
39+ }
13540 }
13641}
0 commit comments