File: batch_cursor_test.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.17.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,988 kB
  • sloc: perl: 533; ansic: 491; python: 432; sh: 327; makefile: 174
file content (133 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (C) MongoDB, Inc. 2022-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

package driver

import (
	"testing"
	"time"

	"go.mongodb.org/mongo-driver/internal/assert"
)

func TestBatchCursor(t *testing.T) {
	t.Parallel()

	t.Run("setBatchSize", func(t *testing.T) {
		t.Parallel()

		var size int32
		bc := &BatchCursor{
			batchSize: size,
		}
		assert.Equal(t, size, bc.batchSize, "expected batchSize %v, got %v", size, bc.batchSize)

		size = int32(4)
		bc.SetBatchSize(size)
		assert.Equal(t, size, bc.batchSize, "expected batchSize %v, got %v", size, bc.batchSize)
	})

	t.Run("calcGetMoreBatchSize", func(t *testing.T) {
		t.Parallel()

		for _, tcase := range []struct {
			name                               string
			size, limit, numReturned, expected int32
			ok                                 bool
		}{
			{
				name:     "empty",
				expected: 0,
				ok:       true,
			},
			{
				name:     "batchSize NEQ 0",
				size:     4,
				expected: 4,
				ok:       true,
			},
			{
				name:     "limit NEQ 0",
				limit:    4,
				expected: 0,
				ok:       true,
			},
			{
				name:        "limit NEQ and batchSize + numReturned EQ limit",
				size:        4,
				limit:       8,
				numReturned: 4,
				expected:    4,
				ok:          true,
			},
			{
				name:        "limit makes batchSize negative",
				numReturned: 4,
				limit:       2,
				expected:    -2,
				ok:          false,
			},
		} {
			tcase := tcase
			t.Run(tcase.name, func(t *testing.T) {
				t.Parallel()

				bc := &BatchCursor{
					limit:       tcase.limit,
					batchSize:   tcase.size,
					numReturned: tcase.numReturned,
				}

				bc.SetBatchSize(tcase.size)

				size, ok := calcGetMoreBatchSize(*bc)

				assert.Equal(t, tcase.expected, size, "expected batchSize %v, got %v", tcase.expected, size)
				assert.Equal(t, tcase.ok, ok, "expected ok %v, got %v", tcase.ok, ok)
			})
		}
	})
}

func TestBatchCursorSetMaxTime(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		dur  time.Duration
		want int64
	}{
		{
			name: "empty",
			dur:  0,
			want: 0,
		},
		{
			name: "partial milliseconds are truncated",
			dur:  10_900 * time.Microsecond,
			want: 10,
		},
		{
			name: "millisecond input",
			dur:  10 * time.Millisecond,
			want: 10,
		},
	}

	for _, test := range tests {
		test := test

		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

			bc := BatchCursor{}
			bc.SetMaxTime(test.dur)

			got := bc.maxTimeMS
			assert.Equal(t, test.want, got, "expected and actual maxTimeMS are different")
		})
	}
}