...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/topology/diff_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/topology

     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  
     7  package topology
     8  
     9  import (
    10  	"testing"
    11  
    12  	"go.mongodb.org/mongo-driver/internal/assert"
    13  	"go.mongodb.org/mongo-driver/mongo/description"
    14  )
    15  
    16  func TestDiffHostList(t *testing.T) {
    17  	h1 := "1.0.0.0:27017"
    18  	h2 := "2.0.0.0:27017"
    19  	h3 := "3.0.0.0:27017"
    20  	h4 := "4.0.0.0:27017"
    21  	h5 := "5.0.0.0:27017"
    22  	h6 := "6.0.0.0:27017"
    23  	s1 := description.Server{Addr: "1.0.0.0:27017"}
    24  	s2 := description.Server{Addr: "2.0.0.0:27017"}
    25  	s3 := description.Server{Addr: "3.0.0.0:27017"}
    26  	s6 := description.Server{Addr: "6.0.0.0:27017"}
    27  
    28  	topo := description.Topology{
    29  		Servers: []description.Server{s6, s1, s3, s2},
    30  	}
    31  	hostlist := []string{h2, h4, h3, h5}
    32  
    33  	diff := diffHostList(topo, hostlist)
    34  
    35  	assert.ElementsMatch(t, []string{h4, h5}, diff.Added)
    36  	assert.ElementsMatch(t, []string{h1, h6}, diff.Removed)
    37  
    38  	// Ensure that original topology servers and hostlist were not reordered.
    39  	assert.EqualValues(t, []description.Server{s6, s1, s3, s2}, topo.Servers)
    40  	assert.EqualValues(t, []string{h2, h4, h3, h5}, hostlist)
    41  }
    42  
    43  func TestDiffTopology(t *testing.T) {
    44  	s1 := description.Server{Addr: "1.0.0.0:27017"}
    45  	s2 := description.Server{Addr: "2.0.0.0:27017"}
    46  	s3 := description.Server{Addr: "3.0.0.0:27017"}
    47  	s4 := description.Server{Addr: "4.0.0.0:27017"}
    48  	s5 := description.Server{Addr: "5.0.0.0:27017"}
    49  	s6 := description.Server{Addr: "6.0.0.0:27017"}
    50  
    51  	t1 := description.Topology{
    52  		Servers: []description.Server{s6, s1, s3, s2},
    53  	}
    54  	t2 := description.Topology{
    55  		Servers: []description.Server{s2, s4, s3, s5},
    56  	}
    57  
    58  	diff := diffTopology(t1, t2)
    59  
    60  	assert.ElementsMatch(t, []description.Server{s4, s5}, diff.Added)
    61  	assert.ElementsMatch(t, []description.Server{s1, s6}, diff.Removed)
    62  
    63  	// Ensure that original topology servers were not reordered.
    64  	assert.EqualValues(t, []description.Server{s6, s1, s3, s2}, t1.Servers)
    65  	assert.EqualValues(t, []description.Server{s2, s4, s3, s5}, t2.Servers)
    66  }
    67  

View as plain text