Skip to content

Commit

Permalink
Put missing replica? in ServerDescription.parse_hello_response/1 and …
Browse files Browse the repository at this point in the history
…add test.
  • Loading branch information
ColaCheng committed Feb 6, 2025
1 parent 0727fbc commit 940af75
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongo/server_description.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ defmodule Mongo.ServerDescription do
compression: map_compressors(hello_response["compression"]),
read_only: hello_response["readOnly"] || false,
logical_session_timeout: hello_response["logicalSessionTimeoutMinutes"] || 30,
supports_retryable_writes: supports_retryable_writes
supports_retryable_writes: supports_retryable_writes,
replica?: replica?(server_type)
}
end

Expand Down
8 changes: 8 additions & 0 deletions test/mongo/topology_description_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,12 @@ defmodule Mongo.TopologyDescriptionTest do

assert :single = TopologyDescription.get_type(opts)
end

test "Set read_preference to :primaryPreferred when topology is single and server is replica set" do
assert {:ok, {_, opts}} = TopologyDescription.select_servers(single(), :read, [])
assert nil == Keyword.get(opts, :read_preference)

assert {:ok, {_, opts}} = TopologyDescription.select_servers(single_with_repl_set(), :read, [])
assert :primaryPreferred = Keyword.get(opts, :read_preference) |> Map.get(:mode)
end
end
35 changes: 35 additions & 0 deletions test/support/topology_test_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ defmodule Mongo.TopologyTestData do
}
}

def single_with_repl_set(),
do: %{
set_name: nil,
type: :single,
compatibility_error: nil,
compatible: true,
local_threshold_ms: 15,
max_election_id: nil,
max_set_version: nil,
servers: %{
"localhost:27017" => %{
address: "localhost:27017",
arbiters: [],
election_id: nil,
error: nil,
hosts: [],
last_update_time: nil,
last_write_date: nil,
max_wire_version: 4,
me: nil,
min_wire_version: 0,
op_time: nil,
passives: [],
primary: nil,
round_trip_time: 44,
set_name: nil,
set_version: nil,
tag_set: %{},
type: :standalone,
compression: [],
replica?: true
}
}
}

def sharded(),
do: %{
set_name: nil,
Expand Down

0 comments on commit 940af75

Please sign in to comment.