summaryrefslogtreecommitdiff
path: root/test/framework/fixtures/fixture.go
blob: a307534d3f2795bcfb8adef28afa6784e108d36a (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package fixtures

import (
	"testing"

	"github.com/algorand/go-deadlock"
)

// TestingTB is identical to testing.TB, beside the private method.
type TestingTB interface {
	Cleanup(func())
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fail()
	FailNow()
	Failed() bool
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Helper()
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	Name() string
	Skip(args ...interface{})
	SkipNow()
	Skipf(format string, args ...interface{})
	Skipped() bool
}

// Fixture provides the base interface for all E2E test fixtures
// so we can work with them abstractly if needed
type Fixture interface {
	// Run executes the tests after the fixture initializes
	// and returns the exit code from testing
	Run(m *testing.M) int

	// Run executes the tests after the fixture initializes, and either
	// returns the exit code from testing, or calls os.Exit(ret) directly
	RunAndExit(m *testing.M)

	// Shutdown should be called for a single-use fixture shutdown / cleanup
	// It requires a valid t.Testing to be assigned.
	Shutdown()

	// ShutdownImpl should not generally be called except for implementations
	// where there is no single t.Testing associated with the fixture
	// (e.g. shared across all tests in a package)
	ShutdownImpl(preserveData bool)
}

var synchTestMu deadlock.Mutex
var synchTests = make(map[TestingTB]TestingTB)

// SynchronizedTest generates a testing.TB compatible test for a given testing.TB interface.
// calling SynchronizedTest with the same tb would return the exact same instance of synchTest
func SynchronizedTest(tb TestingTB) TestingTB {
	if st, ok := tb.(*synchTest); ok {
		return st
	}
	synchTestMu.Lock()
	defer synchTestMu.Unlock()
	if t, have := synchTests[tb]; have {
		return t
	}
	t := &synchTest{
		t: tb,
	}
	synchTests[tb] = t
	return t
}

type synchTest struct {
	deadlock.Mutex
	t                  TestingTB
	dontReportFailures bool
}

// ShutdownSynchronizedTest should be called within each test using synchTest.
// It ensures the base test will no longer get t.finished modified and cause a data race.
// It should be called in the form of "defer fixtures.ShutdownSynchronizedTest(t)"
func ShutdownSynchronizedTest(t TestingTB) {
	st := SynchronizedTest(t).(*synchTest)
	st.Lock()
	defer st.Unlock()
	st.dontReportFailures = true
}

func (st *synchTest) Cleanup(f func()) {
	st.Lock()
	defer st.Unlock()
	st.t.Cleanup(f)
}
func (st *synchTest) Error(args ...interface{}) {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.t.Error(args...)
	}
}
func (st *synchTest) Errorf(format string, args ...interface{}) {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.t.Errorf(format, args...)
	}
}
func (st *synchTest) Fail() {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.t.Fail()
	}
}
func (st *synchTest) FailNow() {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.dontReportFailures = true
		st.t.FailNow()
	}
}
func (st *synchTest) Failed() bool {
	st.Lock()
	defer st.Unlock()
	return st.t.Failed()
}
func (st *synchTest) Fatal(args ...interface{}) {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.dontReportFailures = true
		st.t.Fatal(args...)
	}
}
func (st *synchTest) Fatalf(format string, args ...interface{}) {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.dontReportFailures = true
		st.t.Fatalf(format, args...)
	}
}
func (st *synchTest) Helper() {
	st.Lock()
	defer st.Unlock()
	st.t.Helper()
}
func (st *synchTest) Log(args ...interface{}) {
	st.t.Helper()
	st.Lock()
	defer st.Unlock()
	st.t.Log(args...)
}
func (st *synchTest) Logf(format string, args ...interface{}) {
	st.t.Helper()
	st.Lock()
	defer st.Unlock()
	st.t.Logf(format, args...)
}
func (st *synchTest) Name() string {
	st.Lock()
	defer st.Unlock()
	return st.t.Name()
}
func (st *synchTest) Skip(args ...interface{}) {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.dontReportFailures = true
		st.t.Skip(args...)
	}
}
func (st *synchTest) SkipNow() {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.dontReportFailures = true
		st.t.SkipNow()
	}
}
func (st *synchTest) Skipf(format string, args ...interface{}) {
	st.Lock()
	defer st.Unlock()
	if !st.dontReportFailures {
		st.dontReportFailures = true
		st.t.Skipf(format, args...)
	}
}
func (st *synchTest) Skipped() bool {
	st.Lock()
	defer st.Unlock()
	return st.t.Skipped()
}