File tree Expand file tree Collapse file tree 3 files changed +70
-11
lines changed
spring-core/src/test/java/org/springframework/core/io/buffer Expand file tree Collapse file tree 3 files changed +70
-11
lines changed Original file line number Diff line number Diff line change @@ -44,11 +44,14 @@ class LeakAwareDataBuffer implements PooledDataBuffer {
44
44
Assert .notNull (dataBufferFactory , "DataBufferFactory must not be null" );
45
45
this .delegate = delegate ;
46
46
this .dataBufferFactory = dataBufferFactory ;
47
- this .leakError = createLeakError ();
47
+ this .leakError = createLeakError (delegate );
48
48
}
49
49
50
- private static AssertionError createLeakError () {
51
- AssertionError result = new AssertionError ("Leak detected in test case" );
50
+ private static AssertionError createLeakError (DataBuffer delegate ) {
51
+ String message = String .format ("DataBuffer leak detected: {%s} has not been released.%n" +
52
+ "Stack trace of buffer allocation statement follows:" ,
53
+ delegate );
54
+ AssertionError result = new AssertionError (message );
52
55
// remove first four irrelevant stack trace elements
53
56
StackTraceElement [] oldTrace = result .getStackTrace ();
54
57
StackTraceElement [] newTrace = new StackTraceElement [oldTrace .length - 4 ];
Original file line number Diff line number Diff line change 20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
22
23
+ import org .jetbrains .annotations .NotNull ;
23
24
import org .junit .After ;
24
25
25
26
import org .springframework .util .Assert ;
@@ -74,17 +75,18 @@ public void checkForLeaks() {
74
75
}
75
76
76
77
@ Override
77
- public LeakAwareDataBuffer allocateBuffer () {
78
- LeakAwareDataBuffer dataBuffer =
79
- new LeakAwareDataBuffer (this .delegate .allocateBuffer (), this );
80
- this .created .add (dataBuffer );
81
- return dataBuffer ;
78
+ public DataBuffer allocateBuffer () {
79
+ return allocateBufferInternal (this .delegate .allocateBuffer ());
82
80
}
83
81
84
82
@ Override
85
- public LeakAwareDataBuffer allocateBuffer (int initialCapacity ) {
86
- LeakAwareDataBuffer dataBuffer =
87
- new LeakAwareDataBuffer (this .delegate .allocateBuffer (initialCapacity ), this );
83
+ public DataBuffer allocateBuffer (int initialCapacity ) {
84
+ return allocateBufferInternal (this .delegate .allocateBuffer (initialCapacity ));
85
+ }
86
+
87
+ @ NotNull
88
+ private DataBuffer allocateBufferInternal (DataBuffer delegateBuffer ) {
89
+ LeakAwareDataBuffer dataBuffer = new LeakAwareDataBuffer (delegateBuffer , this );
88
90
this .created .add (dataBuffer );
89
91
return dataBuffer ;
90
92
}
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2002-2018 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package org .springframework .core .io .buffer ;
18
+
19
+ import org .junit .Test ;
20
+
21
+ import static org .junit .Assert .*;
22
+ import static org .springframework .core .io .buffer .DataBufferUtils .release ;
23
+
24
+ /**
25
+ * @author Arjen Poutsma
26
+ */
27
+ public class LeakAwareDataBufferFactoryTests {
28
+
29
+ private final LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory ();
30
+
31
+
32
+ @ Test
33
+ public void leak () {
34
+ DataBuffer dataBuffer = this .bufferFactory .allocateBuffer ();
35
+ try {
36
+ this .bufferFactory .checkForLeaks ();
37
+ fail ("AssertionError expected" );
38
+ }
39
+ catch (AssertionError expected ) {
40
+ // ignore
41
+ }
42
+ finally {
43
+ release (dataBuffer );
44
+ }
45
+ }
46
+
47
+ @ Test
48
+ public void noLeak () {
49
+ DataBuffer dataBuffer = this .bufferFactory .allocateBuffer ();
50
+ release (dataBuffer );
51
+ this .bufferFactory .checkForLeaks ();
52
+ }
53
+
54
+ }
You can’t perform that action at this time.
0 commit comments