1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package description
8
9import (
10	"errors"
11	"testing"
12	"time"
13
14	"go.mongodb.org/mongo-driver/bson/primitive"
15	"go.mongodb.org/mongo-driver/internal/testutil/assert"
16	"go.mongodb.org/mongo-driver/mongo/address"
17	"go.mongodb.org/mongo-driver/tag"
18)
19
20func TestServer(t *testing.T) {
21	t.Run("equals", func(t *testing.T) {
22		defaultServer := Server{}
23		// Only some of the Server fields affect equality
24		testCases := []struct {
25			name   string
26			server Server
27			equal  bool
28		}{
29			{"empty", Server{}, true},
30			{"address", Server{Addr: address.Address("foo")}, true},
31			{"arbiters", Server{Arbiters: []string{"foo"}}, false},
32			{"rtt", Server{AverageRTT: time.Second}, true},
33			{"compression", Server{Compression: []string{"foo"}}, true},
34			{"canonicalAddr", Server{CanonicalAddr: address.Address("foo")}, false},
35			{"electionID", Server{ElectionID: primitive.NewObjectID()}, false},
36			{"heartbeatInterval", Server{HeartbeatInterval: time.Second}, true},
37			{"hosts", Server{Hosts: []string{"foo"}}, false},
38			{"lastError", Server{LastError: errors.New("foo")}, false},
39			{"lastUpdateTime", Server{LastUpdateTime: time.Now()}, true},
40			{"lastWriteTime", Server{LastWriteTime: time.Now()}, true},
41			{"maxBatchCount", Server{MaxBatchCount: 1}, true},
42			{"maxDocumentSize", Server{MaxDocumentSize: 1}, true},
43			{"maxMessageSize", Server{MaxMessageSize: 1}, true},
44			{"members", Server{Members: []address.Address{address.Address("foo")}}, true},
45			{"passives", Server{Passives: []string{"foo"}}, false},
46			{"primary", Server{Primary: address.Address("foo")}, false},
47			{"readOnly", Server{ReadOnly: true}, true},
48			{"sessionTimeoutMinutes", Server{SessionTimeoutMinutes: 1}, false},
49			{"setName", Server{SetName: "foo"}, false},
50			{"setVersion", Server{SetVersion: 1}, false},
51			{"tags", Server{Tags: tag.Set{tag.Tag{"foo", "bar"}}}, false},
52			{"topologyVersion", Server{TopologyVersion: &TopologyVersion{primitive.NewObjectID(), 0}}, false},
53			{"kind", Server{Kind: Standalone}, false},
54			{"wireVersion", Server{WireVersion: &VersionRange{1, 2}}, false},
55		}
56		for _, tc := range testCases {
57			t.Run(tc.name, func(t *testing.T) {
58				actual := defaultServer.Equal(tc.server)
59				assert.Equal(t, actual, tc.equal, "expected %v, got %v", tc.equal, actual)
60			})
61		}
62	})
63}
64