File: unified_runner_thread_helpers_test.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.8.4%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 18,520 kB
  • sloc: perl: 533; ansic: 491; python: 432; makefile: 187; sh: 72
file content (109 lines) | stat: -rw-r--r-- 3,254 bytes parent folder | download | duplicates (2)
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
// 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

package integration

import (
	"fmt"
	"sync"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/internal/testutil/assert"
	"go.mongodb.org/mongo-driver/mongo/integration/mtest"
)

// Helper functions for the operations in the unified spec test runner that require creating and synchronizing
// background goroutines.

// backgroundRoutine represents a background goroutine that can execute operations. The goroutine reads operations from
// a channel and executes them in order. It exits when an operation with name exitRoutineOperationName is read. If any
// of the operations error, all future operations passed to the routine are skipped. The first error is reported by the
// stop() function.
type backgroundRoutine struct {
	operations chan *operation
	mt         *mtest.T
	testCase   *testCase
	wg         sync.WaitGroup
	err        error
}

func newBackgroundRoutine(mt *mtest.T, testCase *testCase) *backgroundRoutine {
	routine := &backgroundRoutine{
		operations: make(chan *operation, 10),
		mt:         mt,
		testCase:   testCase,
	}

	return routine
}

func (b *backgroundRoutine) start() {
	b.wg.Add(1)

	go func() {
		defer b.wg.Done()

		for op := range b.operations {
			if b.err != nil {
				continue
			}

			if err := runOperation(b.mt, b.testCase, op, nil, nil); err != nil {
				b.err = fmt.Errorf("error running operation %s: %v", op.Name, err)
			}
		}
	}()
}

func (b *backgroundRoutine) stop() error {
	close(b.operations)
	b.wg.Wait()
	return b.err
}

func (b *backgroundRoutine) addOperation(op *operation) bool {
	select {
	case b.operations <- op:
		return true
	default:
		return false
	}
}

func startThread(mt *mtest.T, testCase *testCase, op *operation) {
	routine := newBackgroundRoutine(mt, testCase)
	testCase.routinesMap.Store(getThreadName(op), routine)
	routine.start()
}

func runOnThread(mt *mtest.T, testCase *testCase, op *operation) {
	routineName := getThreadName(op)
	routineVal, ok := testCase.routinesMap.Load(routineName)
	assert.True(mt, ok, "no background routine found with name %s", routineName)
	routine := routineVal.(*backgroundRoutine)

	var routineOperation operation
	operationDoc := op.Arguments.Lookup("operation")
	err := bson.UnmarshalWithRegistry(specTestRegistry, operationDoc.Document(), &routineOperation)
	assert.Nil(mt, err, "error creating operation for runOnThread: %v", err)

	ok = routine.addOperation(&routineOperation)
	assert.True(mt, ok, "failed to add operation %s to routine %s", routineOperation.Name, routineName)
}

func waitForThread(mt *mtest.T, testCase *testCase, op *operation) {
	name := getThreadName(op)
	routineVal, ok := testCase.routinesMap.Load(name)
	assert.True(mt, ok, "no background routine found with name %s", name)

	err := routineVal.(*backgroundRoutine).stop()
	testCase.routinesMap.Delete(name)
	assert.Nil(mt, err, "error on background routine %s: %v", name, err)
}

func getThreadName(op *operation) string {
	return op.Arguments.Lookup("name").StringValue()
}