...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/session/cluster_clock_test.go

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

     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 session
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    14  )
    15  
    16  func TestClusterClock(t *testing.T) {
    17  	var clusterTime1 = bsoncore.BuildDocument(nil, bsoncore.AppendDocumentElement(nil, "$clusterTime", bsoncore.BuildDocument(nil, bsoncore.AppendTimestampElement(nil, "clusterTime", 10, 5))))
    18  	var clusterTime2 = bsoncore.BuildDocument(nil, bsoncore.AppendDocumentElement(nil, "$clusterTime", bsoncore.BuildDocument(nil, bsoncore.AppendTimestampElement(nil, "clusterTime", 5, 5))))
    19  	var clusterTime3 = bsoncore.BuildDocument(nil, bsoncore.AppendDocumentElement(nil, "$clusterTime", bsoncore.BuildDocument(nil, bsoncore.AppendTimestampElement(nil, "clusterTime", 5, 0))))
    20  
    21  	t.Run("ClusterTime", func(t *testing.T) {
    22  		clock := ClusterClock{}
    23  		clock.AdvanceClusterTime(clusterTime3)
    24  		done := make(chan struct{})
    25  		go func() {
    26  			clock.AdvanceClusterTime(clusterTime1)
    27  			done <- struct{}{}
    28  		}()
    29  		clock.AdvanceClusterTime(clusterTime2)
    30  
    31  		<-done
    32  		if !bytes.Equal(clock.GetClusterTime(), clusterTime1) {
    33  			t.Errorf("Expected cluster time %v, received %v", clusterTime1, clock.GetClusterTime())
    34  		}
    35  	})
    36  }
    37  

View as plain text