Skip to content

Commit 744da27

Browse files
committed
Return PausedDuration from pauseInternal() method
1 parent c233242 commit 744da27

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

lib/src/main/java/com/scalar/admin/kubernetes/Pauser.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public class Pauser {
3636
@VisibleForTesting static final int MAX_UNPAUSE_RETRY_COUNT = 3;
3737

3838
private final TargetSelector targetSelector;
39-
private Instant startTime;
40-
private Instant endTime;
4139

4240
/**
4341
* @param namespace The namespace where the pods are deployed.
@@ -94,9 +92,10 @@ public PausedDuration pause(int pauseDuration, @Nullable Long maxPauseWaitTime)
9492
}
9593

9694
// Run a pause operation.
95+
PausedDuration pausedDuration = null;
9796
PauseFailedException pauseFailedException = null;
9897
try {
99-
pauseInternal(requestCoordinator, pauseDuration, maxPauseWaitTime);
98+
pausedDuration = pauseInternal(requestCoordinator, pauseDuration, maxPauseWaitTime);
10099
} catch (Exception e) {
101100
pauseFailedException = new PauseFailedException("Pause operation failed.", e);
102101
}
@@ -131,10 +130,8 @@ public PausedDuration pause(int pauseDuration, @Nullable Long maxPauseWaitTime)
131130
"The target pods were updated during the pause duration. You cannot use the backup that"
132131
+ " was taken during this pause duration. ";
133132
String unpauseFailedButPauseOkErrorMessage =
134-
String.format(
135-
"Note that the pause operations for taking backup succeeded. You can use a backup that"
136-
+ " was taken during this pause duration: Start Time = %s, End Time = %s. ",
137-
startTime, endTime);
133+
"Note that the pause operations for taking backup succeeded. You can use a backup that"
134+
+ " was taken during this pause duration. ";
138135

139136
// Get pods and deployment information after pause.
140137
TargetSnapshot targetAfterPause;
@@ -196,7 +193,7 @@ public PausedDuration pause(int pauseDuration, @Nullable Long maxPauseWaitTime)
196193
// issue might be caused by temporary issue, for example, pod restarts.
197194
throw new PauseFailedException(errorMessage);
198195
} else { // All operations succeeded.
199-
return new PausedDuration(startTime, endTime);
196+
return pausedDuration;
200197
}
201198
}
202199

@@ -229,13 +226,13 @@ RequestCoordinator getRequestCoordinator(TargetSnapshot target) {
229226
}
230227

231228
@VisibleForTesting
232-
void pauseInternal(
229+
PausedDuration pauseInternal(
233230
RequestCoordinator requestCoordinator, int pauseDuration, @Nullable Long maxPauseWaitTime) {
234-
235231
requestCoordinator.pause(true, maxPauseWaitTime);
236-
startTime = Instant.now();
232+
Instant startTime = Instant.now();
237233
Uninterruptibles.sleepUninterruptibly(pauseDuration, TimeUnit.MILLISECONDS);
238-
endTime = Instant.now();
234+
Instant endTime = Instant.now();
235+
return new PausedDuration(startTime, endTime);
239236
}
240237

241238
@VisibleForTesting

lib/src/test/java/com/scalar/admin/kubernetes/PauserTest.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,15 @@ void pause_WhenUnpauseWithRetryThrowException_ShouldThrowUnpauseFailedException(
312312
String namespace = "dummyNs";
313313
String helmReleaseName = "dummyRelease";
314314
int pauseDuration = 1;
315-
316315
Instant startTime = Instant.now().minus(5, SECONDS);
317316
Instant endTime = Instant.now().plus(5, SECONDS);
318-
MockedStatic<Instant> mockedTime = mockStatic(Instant.class);
319-
mockedTime.when(() -> Instant.now()).thenReturn(startTime).thenReturn(endTime);
320317

321318
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
319+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
320+
322321
doReturn(targetBeforePause).doReturn(targetAfterPause).when(pauser).getTarget();
323322
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
324-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
323+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
325324
doThrow(RuntimeException.class)
326325
.when(pauser)
327326
.unpauseWithRetry(requestCoordinator, MAX_UNPAUSE_RETRY_COUNT);
@@ -335,12 +334,9 @@ void pause_WhenUnpauseWithRetryThrowException_ShouldThrowUnpauseFailedException(
335334
"Unpause operation failed. Scalar products might still be in a paused state. You must"
336335
+ " restart related pods by using the `kubectl rollout restart deployment %s`"
337336
+ " command to unpause all pods. Note that the pause operations for taking backup"
338-
+ " succeeded. You can use a backup that was taken during this pause duration:"
339-
+ " Start Time = %s, End Time = %s. ",
337+
+ " succeeded. You can use a backup that was taken during this pause duration. ",
340338
DUMMY_OBJECT_NAME, startTime, endTime),
341339
thrown.getMessage());
342-
343-
mockedTime.close();
344340
}
345341

346342
@Test
@@ -350,10 +346,15 @@ void pause_WhenSecondGetTargetThrowException_ShouldThrowPauserException()
350346
String namespace = "dummyNs";
351347
String helmReleaseName = "dummyRelease";
352348
int pauseDuration = 1;
349+
Instant startTime = Instant.now().minus(5, SECONDS);
350+
Instant endTime = Instant.now().plus(5, SECONDS);
351+
353352
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
353+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
354+
354355
doReturn(targetBeforePause).doThrow(RuntimeException.class).when(pauser).getTarget();
355356
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
356-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
357+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
357358
doNothing().when(pauser).unpauseWithRetry(any(), anyInt());
358359

359360
// Act & Assert
@@ -373,10 +374,15 @@ void pause_WhenSecondGetTargetThrowException_ShouldThrowPauserException()
373374
String namespace = "dummyNs";
374375
String helmReleaseName = "dummyRelease";
375376
int pauseDuration = 1;
377+
Instant startTime = Instant.now().minus(5, SECONDS);
378+
Instant endTime = Instant.now().plus(5, SECONDS);
379+
376380
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
381+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
382+
377383
doReturn(targetBeforePause).doThrow(RuntimeException.class).when(pauser).getTarget();
378384
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
379-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
385+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
380386
doThrow(RuntimeException.class)
381387
.when(pauser)
382388
.unpauseWithRetry(requestCoordinator, MAX_UNPAUSE_RETRY_COUNT);
@@ -400,10 +406,15 @@ void pause_WhenTargetStatusEqualsThrowException_ShouldThrowStatusCheckFailedExce
400406
String namespace = "dummyNs";
401407
String helmReleaseName = "dummyRelease";
402408
int pauseDuration = 1;
409+
Instant startTime = Instant.now().minus(5, SECONDS);
410+
Instant endTime = Instant.now().plus(5, SECONDS);
411+
403412
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
413+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
414+
404415
doReturn(targetBeforePause).doReturn(targetAfterPause).when(pauser).getTarget();
405416
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
406-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
417+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
407418
doNothing().when(pauser).unpauseWithRetry(any(), anyInt());
408419
doThrow(RuntimeException.class).when(pauser).targetStatusEquals(any(), any());
409420

@@ -425,10 +436,15 @@ void pause_WhenTargetStatusEqualsThrowException_ShouldThrowStatusCheckFailedExce
425436
String namespace = "dummyNs";
426437
String helmReleaseName = "dummyRelease";
427438
int pauseDuration = 1;
439+
Instant startTime = Instant.now().minus(5, SECONDS);
440+
Instant endTime = Instant.now().plus(5, SECONDS);
441+
428442
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
443+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
444+
429445
doReturn(targetBeforePause).doReturn(targetAfterPause).when(pauser).getTarget();
430446
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
431-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
447+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
432448
doThrow(RuntimeException.class)
433449
.when(pauser)
434450
.unpauseWithRetry(requestCoordinator, MAX_UNPAUSE_RETRY_COUNT);
@@ -453,7 +469,12 @@ void pause_WhenTargetPodStatusChanged_ShouldThrowStatusCheckFailedException()
453469
String namespace = "dummyNs";
454470
String helmReleaseName = "dummyRelease";
455471
int pauseDuration = 1;
472+
Instant startTime = Instant.now().minus(5, SECONDS);
473+
Instant endTime = Instant.now().plus(5, SECONDS);
474+
456475
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
476+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
477+
457478
doReturn(targetBeforePause).doReturn(targetAfterPause).when(pauser).getTarget();
458479
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
459480

@@ -476,7 +497,7 @@ void pause_WhenTargetPodStatusChanged_ShouldThrowStatusCheckFailedException()
476497
doReturn(beforeTargetStatus).when(targetBeforePause).getStatus();
477498
doReturn(afterTargetStatus).when(targetAfterPause).getStatus();
478499

479-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
500+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
480501
doNothing().when(pauser).unpauseWithRetry(any(), anyInt());
481502

482503
// Act & Assert
@@ -528,10 +549,15 @@ void pause_WhenUnpauseFailedAndTargetPodStatusChanged_ShouldThrowUnpauseFailedEx
528549
String namespace = "dummyNs";
529550
String helmReleaseName = "dummyRelease";
530551
int pauseDuration = 1;
552+
Instant startTime = Instant.now().minus(5, SECONDS);
553+
Instant endTime = Instant.now().plus(5, SECONDS);
554+
531555
Pauser pauser = spy(new Pauser(namespace, helmReleaseName));
556+
PausedDuration pausedDuration = new PausedDuration(startTime, endTime);
557+
532558
doReturn(targetBeforePause).doReturn(targetAfterPause).when(pauser).getTarget();
533559
doReturn(requestCoordinator).when(pauser).getRequestCoordinator(targetBeforePause);
534-
doNothing().when(pauser).pauseInternal(any(), anyInt(), anyLong());
560+
doReturn(pausedDuration).when(pauser).pauseInternal(any(), anyInt(), anyLong());
535561
doThrow(RuntimeException.class)
536562
.when(pauser)
537563
.unpauseWithRetry(requestCoordinator, MAX_UNPAUSE_RETRY_COUNT);

0 commit comments

Comments
 (0)