@@ -6,56 +6,70 @@ import TestsCommon
66import XCTest
77
88final class ServerSelectionTests : MongoSwiftTestCase {
9- func testServerSelection( ) throws {
10- let standaloneServer = ServerDescription ( type: . standalone)
11- let rsPrimaryServer = ServerDescription ( type: . rsPrimary)
12- let rsSecondaryServer1 = ServerDescription ( type: . rsSecondary)
13- let rsSecondaryServer2 = ServerDescription ( type: . rsSecondary)
14- let mongosServer = ServerDescription ( type: . mongos)
15-
16- // unknown
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( ) {
1727 let unkownTopology = TopologyDescription ( type: . unknown, servers: [ standaloneServer] )
1828 expect ( unkownTopology. findSuitableServers ( ) ) . to ( haveCount ( 0 ) )
29+ }
1930
20- // single
31+ func testSingleTopology ( ) {
2132 let singleTopology = TopologyDescription ( type: . single, servers: [ standaloneServer] )
2233 expect ( singleTopology. findSuitableServers ( ) [ 0 ] . type) . to ( equal ( . standalone) )
34+ }
2335
24- // replica set with primary
36+ func testReplicaSetWithPrimaryTopology ( ) {
2537 let replicaSetTopology = TopologyDescription ( type: . replicaSetWithPrimary, servers: [
2638 rsPrimaryServer,
2739 rsSecondaryServer1,
2840 rsSecondaryServer2
2941 ] )
30- let primaryReadPreference = ReadPreference ( . primary )
31- let replicaSetSuitableServers = replicaSetTopology . findSuitableServers ( readPreference: primaryReadPreference)
42+ let replicaSetSuitableServers = replicaSetTopology
43+ . findSuitableServers ( readPreference: self . primaryReadPreference)
3244 expect ( replicaSetSuitableServers [ 0 ] . type) . to ( equal ( . rsPrimary) )
3345 expect ( replicaSetSuitableServers) . to ( haveCount ( 1 ) )
3446
35- let primaryPrefReadPreferemce = ReadPreference ( . primaryPreferred)
3647 let replicaSetSuitableServers2 = replicaSetTopology
37- . findSuitableServers ( readPreference: primaryPrefReadPreferemce)
48+ . findSuitableServers ( readPreference: self . primaryPrefReadPreferemce)
3849 expect ( replicaSetSuitableServers2 [ 0 ] . type) . to ( equal ( . rsPrimary) )
3950 expect ( replicaSetSuitableServers2) . to ( haveCount ( 1 ) )
51+ }
4052
41- // replica set without primary
53+ func testReplicaSetNoPrimaryTopology ( ) {
4254 let replicaSetNoPrimaryTopology = TopologyDescription ( type: . replicaSetNoPrimary, servers: [
4355 rsSecondaryServer1,
4456 rsSecondaryServer2
4557 ] )
46- let replicaSetNoPrimarySuitableServers = replicaSetNoPrimaryTopology
47- . findSuitableServers ( readPreference: primaryReadPreference)
48- expect ( replicaSetNoPrimarySuitableServers ) . to ( haveCount ( 0 ) )
58+ let suitable1 = replicaSetNoPrimaryTopology
59+ . findSuitableServers ( readPreference: self . primaryReadPreference)
60+ expect ( suitable1 ) . to ( haveCount ( 0 ) )
4961
50- let replicaSetNoPrimarySuitableServer2 = replicaSetNoPrimaryTopology. findSuitableServers ( readPreference: nil )
51- expect ( replicaSetNoPrimarySuitableServer2) . to ( haveCount ( 0 ) )
62+ let suitable2 = replicaSetNoPrimaryTopology
63+ . findSuitableServers ( readPreference: nil )
64+ expect ( suitable2) . to ( haveCount ( 0 ) )
5265
53- let replicaSetNoPrimarySuitableServer3 = replicaSetNoPrimaryTopology
54- . findSuitableServers ( readPreference: primaryPrefReadPreferemce)
55- expect ( replicaSetNoPrimarySuitableServer3 [ 0 ] . type) . to ( equal ( . rsSecondary) )
56- expect ( replicaSetNoPrimarySuitableServer3) . to ( haveCount ( 2 ) )
66+ let suitable3 = replicaSetNoPrimaryTopology
67+ . findSuitableServers ( readPreference: self . primaryPrefReadPreferemce)
68+ expect ( suitable3 [ 0 ] . type) . to ( equal ( . rsSecondary) )
69+ expect ( suitable3) . to ( haveCount ( 2 ) )
70+ }
5771
58- // sharded
72+ func testShardedTopology ( ) {
5973 let shardedTopology = TopologyDescription ( type: . sharded, servers: [
6074 mongosServer
6175 ] )
@@ -64,4 +78,59 @@ final class ServerSelectionTests: MongoSwiftTestCase {
6478 . to ( equal ( . mongos) )
6579 expect ( shardedSuitableServers) . to ( haveCount ( 1 ) )
6680 }
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 = [ : ]
125+
126+ let primaryReadPreferenceWithEmptyTagSet = try ReadPreference (
127+ . primary,
128+ tagSets: [ emptyTagSet] ,
129+ maxStalenessSeconds: nil
130+ )
131+ let replicaSetSuitableServers = replicaSetTopology
132+ . findSuitableServers ( readPreference: primaryReadPreferenceWithEmptyTagSet)
133+ expect ( replicaSetSuitableServers [ 0 ] . type) . to ( equal ( . rsPrimary) )
134+ expect ( replicaSetSuitableServers) . to ( haveCount ( 1 ) )
135+ }
67136}
0 commit comments