1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// +build go1.13
package topology
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo/description"
)
var selectNone description.ServerSelectorFunc = func(description.Topology, []description.Server) ([]description.Server, error) {
return []description.Server{}, nil
}
func TestTopologyErrors(t *testing.T) {
t.Run("errors are wrapped", func(t *testing.T) {
t.Run("server selection error", func(t *testing.T) {
topo, err := New()
noerr(t, err)
topo.cfg.cs.HeartbeatInterval = time.Minute
atomic.StoreInt64(&topo.connectionstate, connected)
desc := description.Topology{
Servers: []description.Server{},
}
topo.desc.Store(desc)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = topo.SelectServer(ctx, description.WriteSelector())
assert.True(t, errors.Is(err, context.Canceled), "expected error %v, got %v", context.Canceled, err)
})
t.Run("context deadline error", func(t *testing.T) {
topo, err := New()
assert.Nil(t, err, "error creating topology: %v", err)
var serverSelectionErr error
callback := func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
state := newServerSelectionState(selectNone, make(<-chan time.Time))
subCh := make(<-chan description.Topology)
_, serverSelectionErr = topo.selectServerFromSubscription(ctx, subCh, state)
}
assert.Soon(t, callback, 150*time.Millisecond)
assert.True(t, errors.Is(serverSelectionErr, context.DeadlineExceeded), "expected %v, recieved %v",
context.DeadlineExceeded, serverSelectionErr)
})
})
}
|