|
16 | 16 |
|
17 | 17 | package com.mongodb.connection
|
18 | 18 |
|
19 |
| -import com.mongodb.MongoException |
20 | 19 | import com.mongodb.MongoSocketException
|
21 | 20 | import com.mongodb.OperationFunctionalSpecification
|
22 | 21 | import com.mongodb.ServerAddress
|
| 22 | +import com.mongodb.Tag |
| 23 | +import com.mongodb.TagSet |
23 | 24 | import com.mongodb.operation.CommandReadOperation
|
24 | 25 | import org.bson.BsonDocument
|
25 | 26 | import org.bson.BsonInt32
|
26 | 27 | import org.bson.codecs.BsonDocumentCodec
|
| 28 | +import org.bson.types.ObjectId |
27 | 29 |
|
28 | 30 | import java.util.concurrent.CountDownLatch
|
29 |
| -import java.util.concurrent.TimeUnit |
30 | 31 |
|
31 |
| -import static DefaultServerMonitor.exceptionHasChanged |
32 |
| -import static DefaultServerMonitor.stateHasChanged |
33 | 32 | import static com.mongodb.ClusterFixture.getBinding
|
34 | 33 | import static com.mongodb.ClusterFixture.getCredentialList
|
35 | 34 | import static com.mongodb.ClusterFixture.getPrimary
|
36 | 35 | import static com.mongodb.ClusterFixture.getSslSettings
|
| 36 | +import static com.mongodb.connection.DefaultServerMonitor.shouldLogStageChange |
| 37 | +import static com.mongodb.connection.ServerConnectionState.CONNECTED |
| 38 | +import static com.mongodb.connection.ServerConnectionState.CONNECTING |
| 39 | +import static com.mongodb.connection.ServerDescription.builder |
| 40 | +import static java.util.Arrays.asList |
37 | 41 |
|
38 | 42 | class ServerMonitorSpecification extends OperationFunctionalSpecification {
|
39 | 43 | ServerDescription newDescription
|
@@ -84,70 +88,121 @@ class ServerMonitorSpecification extends OperationFunctionalSpecification {
|
84 | 88 | newDescription.exception instanceof MongoSocketException
|
85 | 89 | }
|
86 | 90 |
|
87 |
| - def 'should report exception has changed when the current and previous are different'() { |
88 |
| - expect: |
89 |
| - exceptionHasChanged(null, new NullPointerException()) |
90 |
| - exceptionHasChanged(new NullPointerException(), null) |
91 |
| - exceptionHasChanged(new SocketException(), new SocketException('A message')) |
92 |
| - exceptionHasChanged(new SocketException('A message'), new SocketException()) |
93 |
| - exceptionHasChanged(new SocketException('A message'), new MongoException('A message')) |
94 |
| - exceptionHasChanged(new SocketException('A message'), new SocketException('A different message')) |
95 |
| - } |
| 91 | + def 'should log state change if significant properties have changed'() { |
| 92 | + given: |
| 93 | + ServerDescription.Builder builder = createBuilder(); |
| 94 | + ServerDescription description = builder.build(); |
| 95 | + ServerDescription otherDescription |
96 | 96 |
|
97 |
| - def 'should report exception has not changed when the current and previous are the same'() { |
98 | 97 | expect:
|
99 |
| - !exceptionHasChanged(null, null) |
100 |
| - !exceptionHasChanged(new NullPointerException(), new NullPointerException()) |
101 |
| - !exceptionHasChanged(new MongoException('A message'), new MongoException('A message')) |
102 |
| - } |
| 98 | + !shouldLogStageChange(description, builder.build()) |
103 | 99 |
|
104 |
| - def 'should report state has changed if descriptions are different'() { |
105 |
| - expect: |
106 |
| - stateHasChanged(ServerDescription.builder() |
107 |
| - .type(ServerType.UNKNOWN) |
108 |
| - .state(ServerConnectionState.CONNECTING) |
109 |
| - .address(new ServerAddress()) |
110 |
| - .build(), |
111 |
| - ServerDescription.builder() |
112 |
| - .type(ServerType.STANDALONE) |
113 |
| - .state(ServerConnectionState.CONNECTED) |
114 |
| - .address(new ServerAddress()) |
115 |
| - .roundTripTime(5, TimeUnit.MILLISECONDS) |
116 |
| - .build()); |
117 |
| - } |
| 100 | + when: |
| 101 | + otherDescription = createBuilder().address(new ServerAddress('localhost:27018')).build(); |
118 | 102 |
|
119 |
| - def 'should report state has changed if latencies are different'() { |
120 |
| - expect: |
121 |
| - stateHasChanged(ServerDescription.builder() |
122 |
| - .type(ServerType.STANDALONE) |
123 |
| - .state(ServerConnectionState.CONNECTED) |
124 |
| - .address(new ServerAddress()) |
125 |
| - .roundTripTime(5, TimeUnit.MILLISECONDS) |
126 |
| - .build(), |
127 |
| - ServerDescription.builder() |
128 |
| - .type(ServerType.STANDALONE) |
129 |
| - .state(ServerConnectionState.CONNECTED) |
130 |
| - .address(new ServerAddress()) |
131 |
| - .roundTripTime(6, TimeUnit.MILLISECONDS) |
132 |
| - .build()); |
| 103 | + then: |
| 104 | + shouldLogStageChange(description, otherDescription) |
| 105 | + |
| 106 | + when: |
| 107 | + otherDescription = createBuilder().type(ServerType.STANDALONE).build(); |
| 108 | + |
| 109 | + then: |
| 110 | + shouldLogStageChange(description, otherDescription) |
| 111 | + |
| 112 | + when: |
| 113 | + otherDescription = createBuilder().tagSet(null).build(); |
| 114 | + |
| 115 | + then: |
| 116 | + shouldLogStageChange(description, otherDescription) |
| 117 | + |
| 118 | + when: |
| 119 | + otherDescription = createBuilder().setName('test2').build(); |
| 120 | + |
| 121 | + then: |
| 122 | + shouldLogStageChange(description, otherDescription) |
| 123 | + |
| 124 | + when: |
| 125 | + otherDescription = createBuilder().primary('localhost:27018').build(); |
| 126 | + |
| 127 | + then: |
| 128 | + shouldLogStageChange(description, otherDescription) |
| 129 | + |
| 130 | + when: |
| 131 | + otherDescription = createBuilder().canonicalAddress('localhost:27018').build(); |
| 132 | + |
| 133 | + then: |
| 134 | + shouldLogStageChange(description, otherDescription) |
| 135 | + |
| 136 | + when: |
| 137 | + otherDescription = createBuilder().hosts(new HashSet<String>(asList('localhost:27018'))).build(); |
| 138 | + |
| 139 | + then: |
| 140 | + shouldLogStageChange(description, otherDescription) |
| 141 | + |
| 142 | + when: |
| 143 | + otherDescription = createBuilder().arbiters(new HashSet<String>(asList('localhost:27018'))).build(); |
| 144 | + |
| 145 | + then: |
| 146 | + shouldLogStageChange(description, otherDescription) |
| 147 | + |
| 148 | + when: |
| 149 | + otherDescription = createBuilder().passives(new HashSet<String>(asList('localhost:27018'))).build(); |
| 150 | + |
| 151 | + then: |
| 152 | + shouldLogStageChange(description, otherDescription) |
| 153 | + |
| 154 | + when: |
| 155 | + otherDescription = createBuilder().ok(false).build(); |
| 156 | + |
| 157 | + then: |
| 158 | + shouldLogStageChange(description, otherDescription) |
| 159 | + |
| 160 | + when: |
| 161 | + otherDescription = createBuilder().state(CONNECTING).build(); |
| 162 | + |
| 163 | + then: |
| 164 | + shouldLogStageChange(description, otherDescription) |
| 165 | + |
| 166 | + when: |
| 167 | + otherDescription = createBuilder().version(new ServerVersion(asList(2, 6, 1))).build(); |
| 168 | + |
| 169 | + then: |
| 170 | + shouldLogStageChange(description, otherDescription) |
| 171 | + |
| 172 | + when: |
| 173 | + otherDescription = createBuilder().electionId(new ObjectId()).build(); |
| 174 | + |
| 175 | + then: |
| 176 | + shouldLogStageChange(description, otherDescription) |
| 177 | + |
| 178 | + when: |
| 179 | + otherDescription = createBuilder().setVersion(3).build(); |
| 180 | + |
| 181 | + then: |
| 182 | + shouldLogStageChange(description, otherDescription) |
| 183 | + |
| 184 | + // test exception state changes |
| 185 | + shouldLogStageChange(createBuilder().exception(new IOException()).build(), |
| 186 | + createBuilder().exception(new RuntimeException()).build()) |
| 187 | + shouldLogStageChange(createBuilder().exception(new IOException('message one')).build(), |
| 188 | + createBuilder().exception(new IOException('message two')).build()) |
133 | 189 | }
|
134 | 190 |
|
135 |
| - def 'should report state has not changed if descriptions and latencies are the same'() { |
136 |
| - expect: |
137 |
| - !stateHasChanged(ServerDescription.builder() |
138 |
| - .type(ServerType.STANDALONE) |
139 |
| - .state(ServerConnectionState.CONNECTED) |
140 |
| - .address(new ServerAddress()) |
141 |
| - .roundTripTime(5, TimeUnit.MILLISECONDS) |
142 |
| - .lastUpdateTimeNanos(42L) |
143 |
| - .build(), |
144 |
| - ServerDescription.builder() |
145 |
| - .type(ServerType.STANDALONE) |
146 |
| - .state(ServerConnectionState.CONNECTED) |
147 |
| - .address(new ServerAddress()) |
148 |
| - .roundTripTime(5, TimeUnit.MILLISECONDS) |
149 |
| - .lastUpdateTimeNanos(42L) |
150 |
| - .build()); |
| 191 | + private static ServerDescription.Builder createBuilder() { |
| 192 | + builder().ok(true) |
| 193 | + .state(CONNECTED) |
| 194 | + .address(new ServerAddress()) |
| 195 | + .type(ServerType.SHARD_ROUTER) |
| 196 | + .tagSet(new TagSet(asList(new Tag('dc', 'ny')))) |
| 197 | + .setName('test') |
| 198 | + .primary('localhost:27017') |
| 199 | + .canonicalAddress('localhost:27017') |
| 200 | + .hosts(new HashSet<String>(asList('localhost:27017', 'localhost:27018'))) |
| 201 | + .passives(new HashSet<String>(asList('localhost:27019'))) |
| 202 | + .arbiters(new HashSet<String>(asList('localhost:27020'))) |
| 203 | + .version(new ServerVersion(asList(2, 4, 1))) |
| 204 | + .electionId(new ObjectId('abcdabcdabcdabcdabcdabcd')) |
| 205 | + .setVersion(2) |
151 | 206 | }
|
152 | 207 |
|
153 | 208 | def initializeServerMonitor(ServerAddress address) {
|
|
0 commit comments