|
| 1 | +/* |
| 2 | + * Copyright 2015 MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.mongodb.selector |
| 16 | + |
| 17 | +import com.mongodb.ServerAddress |
| 18 | +import com.mongodb.connection.ClusterDescription |
| 19 | +import com.mongodb.connection.ServerDescription |
| 20 | +import com.mongodb.connection.ServerType |
| 21 | +import spock.lang.Specification |
| 22 | + |
| 23 | +import static com.mongodb.ReadPreference.primary |
| 24 | +import static com.mongodb.ReadPreference.secondary |
| 25 | +import static com.mongodb.connection.ClusterConnectionMode.MULTIPLE |
| 26 | +import static com.mongodb.connection.ClusterConnectionMode.SINGLE |
| 27 | +import static com.mongodb.connection.ClusterType.REPLICA_SET |
| 28 | +import static com.mongodb.connection.ServerConnectionState.CONNECTED |
| 29 | + |
| 30 | +class ReadPreferenceServerSelectorSpecification extends Specification { |
| 31 | + |
| 32 | + def primary = ServerDescription.builder() |
| 33 | + .state(CONNECTED) |
| 34 | + .address(new ServerAddress()) |
| 35 | + .ok(true) |
| 36 | + .type(ServerType.REPLICA_SET_PRIMARY) |
| 37 | + .build(); |
| 38 | + def secondary = ServerDescription.builder() |
| 39 | + .state(CONNECTED) |
| 40 | + .address(new ServerAddress('localhost', 27018)) |
| 41 | + .ok(true) |
| 42 | + .type(ServerType.REPLICA_SET_SECONDARY) |
| 43 | + .build(); |
| 44 | + |
| 45 | + def 'constructor should throws if read preference is null'() { |
| 46 | + when: |
| 47 | + new ReadPreferenceServerSelector(null) |
| 48 | + |
| 49 | + then: |
| 50 | + thrown(IllegalArgumentException) |
| 51 | + } |
| 52 | + |
| 53 | + def 'should get read preference'() { |
| 54 | + expect: |
| 55 | + new ReadPreferenceServerSelector(primary()).readPreference == primary() |
| 56 | + } |
| 57 | + |
| 58 | + def 'should override toString'() { |
| 59 | + expect: |
| 60 | + new ReadPreferenceServerSelector(primary()).toString() == 'ReadPreferenceServerSelector{readPreference=primary}' |
| 61 | + } |
| 62 | + |
| 63 | + def 'should select server that matches read preference when connection mode is multiple'() { |
| 64 | + expect: |
| 65 | + new ReadPreferenceServerSelector(primary()).select(new ClusterDescription(MULTIPLE, REPLICA_SET, [primary, secondary])) == |
| 66 | + [primary] |
| 67 | + new ReadPreferenceServerSelector(secondary()).select(new ClusterDescription(MULTIPLE, REPLICA_SET, [primary, secondary])) == |
| 68 | + [secondary] |
| 69 | + } |
| 70 | + |
| 71 | + def 'should select any ok server when connection mode is single'() { |
| 72 | + expect: |
| 73 | + new ReadPreferenceServerSelector(primary()).select(new ClusterDescription(SINGLE, REPLICA_SET, [secondary])) == [secondary] |
| 74 | + } |
| 75 | +} |
0 commit comments