Skip to content

Commit 5a8e3d5

Browse files
committed
Merge pull request #43328 from kgb-financial-com
* pr/43328: Polish 'Accept Docker progress on numbers >2GB' Accept Docker progress on numbers >2GB Closes gh-43328
2 parents 86b7fe4 + 0afbc0b commit 5a8e3d5

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ProgressUpdateEvent.java

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
* An {@link UpdateEvent} that includes progress information.
2323
*
2424
* @author Phillip Webb
25+
* @author Wolfgang Kronberg
2526
* @since 2.3.0
2627
*/
2728
public abstract class ProgressUpdateEvent extends UpdateEvent {
@@ -67,32 +68,55 @@ public String getProgress() {
6768
*/
6869
public static class ProgressDetail {
6970

70-
private final Integer current;
71+
private final Long current;
7172

72-
private final Integer total;
73+
private final Long total;
7374

7475
@JsonCreator
75-
public ProgressDetail(Integer current, Integer total) {
76+
public ProgressDetail(Long current, Long total) {
7677
this.current = current;
7778
this.total = total;
7879
}
7980

8081
/**
8182
* Return the current progress value.
8283
* @return the current progress
84+
* @deprecated since 3.3.7 for removal in 3.5.0 in favor of
85+
* {@link #asPercentage()}
8386
*/
87+
@Deprecated(since = "3.3.7", forRemoval = true)
8488
public int getCurrent() {
85-
return this.current;
89+
return (int) Long.min(this.current, Integer.MAX_VALUE);
8690
}
8791

8892
/**
8993
* Return the total progress possible value.
9094
* @return the total progress possible
95+
* @deprecated since 3.3.7 for removal in 3.5.0 in favor of
96+
* {@link #asPercentage()}
9197
*/
98+
@Deprecated(since = "3.3.7", forRemoval = true)
9299
public int getTotal() {
93-
return this.total;
100+
return (int) Long.min(this.total, Integer.MAX_VALUE);
94101
}
95102

103+
/**
104+
* Return the progress as a percentage.
105+
* @return the progress percentage
106+
* @since 3.3.7
107+
*/
108+
public int asPercentage() {
109+
int percentage = (int) ((100.0 / this.total) * this.current);
110+
return (percentage < 0) ? 0 : Math.min(percentage, 100);
111+
}
112+
113+
/**
114+
* Return if the progress detail is considered empty.
115+
* @param progressDetail the progress detail to check
116+
* @return if the progress detail is empty
117+
* @deprecated since 3.3.7 for removal in 3.5.0
118+
*/
119+
@Deprecated(since = "3.3.7", forRemoval = true)
96120
public static boolean isEmpty(ProgressDetail progressDetail) {
97121
return progressDetail == null || progressDetail.current == null || progressDetail.total == null;
98122
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/TotalProgressListener.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,10 +89,7 @@ private void publish(int fallback) {
8989
}
9090

9191
private static int withinPercentageBounds(int value) {
92-
if (value < 0) {
93-
return 0;
94-
}
95-
return Math.min(value, 100);
92+
return (value < 0) ? 0 : Math.min(value, 100);
9693
}
9794

9895
/**
@@ -115,8 +112,7 @@ void update(ImageProgressUpdateEvent event) {
115112
}
116113

117114
private int updateProgress(int current, ProgressDetail detail) {
118-
int result = withinPercentageBounds((int) ((100.0 / detail.getTotal()) * detail.getCurrent()));
119-
return Math.max(result, current);
115+
return Math.max(detail.asPercentage(), current);
120116
}
121117

122118
void finish() {

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/ProgressUpdateEventTests.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
* @param <E> The event type
2929
* @author Phillip Webb
3030
* @author Scott Frederick
31+
* @author Wolfgang Kronberg
3132
*/
3233
abstract class ProgressUpdateEventTests<E extends ProgressUpdateEvent> {
3334

@@ -38,10 +39,21 @@ void getStatusReturnsStatus() {
3839
}
3940

4041
@Test
42+
@SuppressWarnings("removal")
4143
void getProgressDetailsReturnsProgressDetails() {
4244
ProgressUpdateEvent event = createEvent();
4345
assertThat(event.getProgressDetail().getCurrent()).isOne();
4446
assertThat(event.getProgressDetail().getTotal()).isEqualTo(2);
47+
assertThat(event.getProgressDetail().asPercentage()).isEqualTo(50);
48+
}
49+
50+
@Test
51+
@SuppressWarnings("removal")
52+
void getProgressDetailsReturnsProgressDetailsForLongNumbers() {
53+
ProgressUpdateEvent event = createEvent("status", new ProgressDetail(4000000000L, 8000000000L), "progress");
54+
assertThat(event.getProgressDetail().getCurrent()).isEqualTo(Integer.MAX_VALUE);
55+
assertThat(event.getProgressDetail().getTotal()).isEqualTo(Integer.MAX_VALUE);
56+
assertThat(event.getProgressDetail().asPercentage()).isEqualTo(50);
4557
}
4658

4759
@Test
@@ -51,25 +63,28 @@ void getProgressReturnsProgress() {
5163
}
5264

5365
@Test
66+
@SuppressWarnings("removal")
5467
void progressDetailIsEmptyWhenCurrentIsNullReturnsTrue() {
55-
ProgressDetail detail = new ProgressDetail(null, 2);
68+
ProgressDetail detail = new ProgressDetail(null, 2L);
5669
assertThat(ProgressDetail.isEmpty(detail)).isTrue();
5770
}
5871

5972
@Test
73+
@SuppressWarnings("removal")
6074
void progressDetailIsEmptyWhenTotalIsNullReturnsTrue() {
61-
ProgressDetail detail = new ProgressDetail(1, null);
75+
ProgressDetail detail = new ProgressDetail(1L, null);
6276
assertThat(ProgressDetail.isEmpty(detail)).isTrue();
6377
}
6478

6579
@Test
80+
@SuppressWarnings("removal")
6681
void progressDetailIsEmptyWhenTotalAndCurrentAreNotNullReturnsFalse() {
67-
ProgressDetail detail = new ProgressDetail(1, 2);
82+
ProgressDetail detail = new ProgressDetail(1L, 2L);
6883
assertThat(ProgressDetail.isEmpty(detail)).isFalse();
6984
}
7085

7186
protected E createEvent() {
72-
return createEvent("status", new ProgressDetail(1, 2), "progress");
87+
return createEvent("status", new ProgressDetail(1L, 2L), "progress");
7388
}
7489

7590
protected abstract E createEvent(String status, ProgressDetail progressDetail, String progress);

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/PullUpdateEventTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,13 +30,15 @@
3030
class PullUpdateEventTests extends AbstractJsonTests {
3131

3232
@Test
33+
@SuppressWarnings("removal")
3334
void readValueWhenFullDeserializesJson() throws Exception {
3435
PullImageUpdateEvent event = getObjectMapper().readValue(getContent("pull-update-full.json"),
3536
PullImageUpdateEvent.class);
3637
assertThat(event.getId()).isEqualTo("4f4fb700ef54");
3738
assertThat(event.getStatus()).isEqualTo("Extracting");
3839
assertThat(event.getProgressDetail().getCurrent()).isEqualTo(16);
3940
assertThat(event.getProgressDetail().getTotal()).isEqualTo(32);
41+
assertThat(event.getProgressDetail().asPercentage()).isEqualTo(50);
4042
assertThat(event.getProgress()).isEqualTo("[==================================================>] 32B/32B");
4143
}
4244

0 commit comments

Comments
 (0)