Skip to content

Commit ee4d9aa

Browse files
committed
8323659: LinkedTransferQueue add and put methods call overridable offer
Reviewed-by: alanb
1 parent 5045839 commit ee4d9aa

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

src/java.base/share/classes/java/util/concurrent/LinkedTransferQueue.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,8 @@ public LinkedTransferQueue(Collection<? extends E> c) {
11431143
* @throws NullPointerException if the specified element is null
11441144
*/
11451145
public void put(E e) {
1146-
offer(e);
1146+
Objects.requireNonNull(e);
1147+
xfer(e, -1L);
11471148
}
11481149

11491150
/**
@@ -1156,7 +1157,9 @@ public void put(E e) {
11561157
* @throws NullPointerException if the specified element is null
11571158
*/
11581159
public boolean offer(E e, long timeout, TimeUnit unit) {
1159-
return offer(e);
1160+
Objects.requireNonNull(e);
1161+
xfer(e, -1L);
1162+
return true;
11601163
}
11611164

11621165
/**
@@ -1181,7 +1184,9 @@ public boolean offer(E e) {
11811184
* @throws NullPointerException if the specified element is null
11821185
*/
11831186
public boolean add(E e) {
1184-
return offer(e);
1187+
Objects.requireNonNull(e);
1188+
xfer(e, -1L);
1189+
return true;
11851190
}
11861191

11871192
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8323659
27+
* @summary Ensures that the implementation of LTQ add and put methods does
28+
* not call overridable offer. This test specifically asserts implementation
29+
* details of LTQ. It's not that such impl details cannot change, just that
30+
* such a change should be deliberately done with suitable consideration
31+
* to compatibility.
32+
* @run testng SubclassTest
33+
*/
34+
35+
import java.util.concurrent.LinkedTransferQueue;
36+
import java.util.concurrent.TimeUnit;
37+
import org.testng.annotations.Test;
38+
import static org.testng.Assert.assertEquals;
39+
40+
@Test
41+
public class SubclassTest {
42+
43+
public void testPut() {
44+
var queue = new TestLinkedTransferQueue();
45+
queue.put(new Object());
46+
assertEquals(queue.size(), 1);
47+
}
48+
49+
public void testAdd() {
50+
var queue = new TestLinkedTransferQueue();
51+
queue.add(new Object());
52+
assertEquals(queue.size(), 1);
53+
}
54+
55+
public void testTimedOffer() {
56+
var queue = new TestLinkedTransferQueue();
57+
queue.offer(new Object(), 60, TimeUnit.SECONDS);
58+
assertEquals(queue.size(), 1);
59+
}
60+
61+
static class TestLinkedTransferQueue extends LinkedTransferQueue<Object> {
62+
@Override
63+
public boolean offer(Object obj) {
64+
return false; // simulate fails to add the given obj
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)