Skip to content

Commit 5496c61

Browse files
author
Per Kops
committed
test: update BackgroundServiceBaseTests to reflect new changes and fix issue with accessing dispoed variable from outer scope
1 parent 0a1c033 commit 5496c61

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

test/Atc.Hosting.Tests/BackgroundServiceBaseTests.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public async Task Logging_Start_Retry_Cancel_Stop()
110110
new OptionsWrapper<MyServiceOptions>(options),
111111
tcs.Task);
112112

113+
var serviceName = sut.ServiceName;
114+
113115
// Act
114116
await sut.StartAsync(cts.Token);
115117

@@ -122,23 +124,23 @@ public async Task Logging_Start_Retry_Cancel_Stop()
122124
logger
123125
.Log(
124126
LogLevel.Information,
125-
$"Started worker {sut.ServiceName}. Worker will run with {options.RepeatIntervalSeconds} seconds interval");
127+
$"Started worker {serviceName}. Worker will run with {options.RepeatIntervalSeconds} seconds interval");
126128

127129
logger
128130
.Log(
129131
LogLevel.Warning,
130-
$"Unhandled exception occurred in worker {sut.ServiceName}. Worker will retry after {options.RepeatIntervalSeconds} seconds",
132+
$"Unhandled exception occurred in worker {serviceName}. Worker will retry after {options.RepeatIntervalSeconds} seconds",
131133
exception);
132134

133135
logger
134136
.Log(
135137
LogLevel.Warning,
136-
$"Cancellation invoked for worker {sut.ServiceName}");
138+
$"Cancellation invoked for worker {serviceName}");
137139

138140
logger
139141
.Log(
140142
LogLevel.Information,
141-
$"Stopped worker {sut.ServiceName}");
143+
$"Stopped worker {serviceName}");
142144
});
143145
}
144146

@@ -165,6 +167,8 @@ public async Task Logging_Start_Exception_Stop()
165167
new OptionsWrapper<MyServiceOptions>(options),
166168
tcs.Task);
167169

170+
var serviceName = sut.ServiceName;
171+
168172
// Act
169173
await sut.StartAsync(cts.Token);
170174

@@ -177,18 +181,18 @@ public async Task Logging_Start_Exception_Stop()
177181
logger
178182
.Log(
179183
LogLevel.Information,
180-
$"Started worker {sut.ServiceName}. Worker will run with {options.RepeatIntervalSeconds} seconds interval");
184+
$"Started worker {serviceName}. Worker will run with {options.RepeatIntervalSeconds} seconds interval");
181185

182186
logger
183187
.Log(
184188
LogLevel.Error,
185-
$"Unhandled exception occurred in worker {sut.ServiceName}",
189+
$"Unhandled exception occurred in worker {serviceName}",
186190
exception);
187191

188192
logger
189193
.Log(
190194
LogLevel.Information,
191-
$"Stopped worker {sut.ServiceName}");
195+
$"Stopped worker {serviceName}");
192196
});
193197
}
194198

@@ -217,6 +221,8 @@ public async Task HealthService_Start_Retry_Cancel_Stop()
217221
healthService,
218222
tcs.Task);
219223

224+
var serviceName = sut.ServiceName;
225+
220226
// Act
221227
await sut.StartAsync(cts.Token);
222228

@@ -226,9 +232,10 @@ public async Task HealthService_Start_Retry_Cancel_Stop()
226232
Received.InOrder(
227233
() =>
228234
{
229-
healthService.SetRunningState(sut.ServiceName, true);
230-
healthService.SetRunningState(sut.ServiceName, true);
231-
healthService.SetRunningState(sut.ServiceName, false);
235+
healthService.SetRunningState(serviceName, true);
236+
healthService.SetRunningState(serviceName, true);
237+
healthService.SetRunningState(serviceName, true);
238+
healthService.SetRunningState(serviceName, false);
232239
});
233240
}
234241

@@ -257,6 +264,8 @@ public async Task HealthService_Start_Exception_Stop()
257264
healthService,
258265
tcs.Task);
259266

267+
var serviceName = sut.ServiceName;
268+
260269
// Act
261270
await sut.StartAsync(cts.Token);
262271

@@ -266,9 +275,9 @@ public async Task HealthService_Start_Exception_Stop()
266275
Received.InOrder(
267276
() =>
268277
{
269-
healthService.SetRunningState(sut.ServiceName, true);
270-
healthService.SetRunningState(sut.ServiceName, true);
271-
healthService.SetRunningState(sut.ServiceName, false);
278+
healthService.SetRunningState(serviceName, true);
279+
healthService.SetRunningState(serviceName, true);
280+
healthService.SetRunningState(serviceName, false);
272281
});
273282
}
274283
}

0 commit comments

Comments
 (0)