1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.google.common.util.concurrent;
17
18 import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 import static java.util.concurrent.TimeUnit.NANOSECONDS;
20
21 import junit.framework.*;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.security.CodeSource;
28 import java.security.Permission;
29 import java.security.PermissionCollection;
30 import java.security.Permissions;
31 import java.security.Policy;
32 import java.security.ProtectionDomain;
33 import java.security.SecurityPermission;
34 import java.util.Arrays;
35 import java.util.Date;
36 import java.util.NoSuchElementException;
37 import java.util.PropertyPermission;
38 import java.util.concurrent.*;
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import java.util.concurrent.atomic.AtomicReference;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 abstract class JSR166TestCase extends TestCase {
116 private static final boolean useSecurityManager =
117 Boolean.getBoolean("jsr166.useSecurityManager");
118
119 protected static final boolean expensiveTests =
120 Boolean.getBoolean("jsr166.expensiveTests");
121
122
123
124
125
126 private static final boolean profileTests =
127 Boolean.getBoolean("jsr166.profileTests");
128
129
130
131
132
133 private static final long profileThreshold =
134 Long.getLong("jsr166.profileThreshold", 100);
135
136 protected void runTest() throws Throwable {
137 if (profileTests)
138 runTestProfiled();
139 else
140 super.runTest();
141 }
142
143 protected void runTestProfiled() throws Throwable {
144 long t0 = System.nanoTime();
145 try {
146 super.runTest();
147 } finally {
148 long elapsedMillis =
149 (System.nanoTime() - t0) / (1000L * 1000L);
150 if (elapsedMillis >= profileThreshold)
151 System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public static long SHORT_DELAY_MS;
260 public static long SMALL_DELAY_MS;
261 public static long MEDIUM_DELAY_MS;
262 public static long LONG_DELAY_MS;
263
264
265
266
267
268 protected long getShortDelay() {
269 return 50;
270 }
271
272
273
274
275 protected void setDelays() {
276 SHORT_DELAY_MS = getShortDelay();
277 SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
278 MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
279 LONG_DELAY_MS = SHORT_DELAY_MS * 200;
280 }
281
282
283
284
285
286 long timeoutMillis() {
287 return SHORT_DELAY_MS / 4;
288 }
289
290
291
292
293
294 Date delayedDate(long delayMillis) {
295 return new Date(System.currentTimeMillis() + delayMillis);
296 }
297
298
299
300
301 private final AtomicReference<Throwable> threadFailure
302 = new AtomicReference<Throwable>(null);
303
304
305
306
307
308
309
310 public void threadRecordFailure(Throwable t) {
311 threadFailure.compareAndSet(null, t);
312 }
313
314 public void setUp() {
315 setDelays();
316 }
317
318
319
320
321
322
323
324
325
326
327 public void tearDown() throws Exception {
328 Throwable t = threadFailure.getAndSet(null);
329 if (t != null) {
330 if (t instanceof Error)
331 throw (Error) t;
332 else if (t instanceof RuntimeException)
333 throw (RuntimeException) t;
334 else if (t instanceof Exception)
335 throw (Exception) t;
336 else {
337 AssertionFailedError afe =
338 new AssertionFailedError(t.toString());
339 afe.initCause(t);
340 throw afe;
341 }
342 }
343
344 if (Thread.interrupted())
345 throw new AssertionFailedError("interrupt status set in main thread");
346 }
347
348
349
350
351
352
353 public void threadFail(String reason) {
354 try {
355 fail(reason);
356 } catch (AssertionFailedError t) {
357 threadRecordFailure(t);
358 fail(reason);
359 }
360 }
361
362
363
364
365
366
367 public void threadAssertTrue(boolean b) {
368 try {
369 assertTrue(b);
370 } catch (AssertionFailedError t) {
371 threadRecordFailure(t);
372 throw t;
373 }
374 }
375
376
377
378
379
380
381 public void threadAssertFalse(boolean b) {
382 try {
383 assertFalse(b);
384 } catch (AssertionFailedError t) {
385 threadRecordFailure(t);
386 throw t;
387 }
388 }
389
390
391
392
393
394
395 public void threadAssertNull(Object x) {
396 try {
397 assertNull(x);
398 } catch (AssertionFailedError t) {
399 threadRecordFailure(t);
400 throw t;
401 }
402 }
403
404
405
406
407
408
409 public void threadAssertEquals(long x, long y) {
410 try {
411 assertEquals(x, y);
412 } catch (AssertionFailedError t) {
413 threadRecordFailure(t);
414 throw t;
415 }
416 }
417
418
419
420
421
422
423 public void threadAssertEquals(Object x, Object y) {
424 try {
425 assertEquals(x, y);
426 } catch (AssertionFailedError t) {
427 threadRecordFailure(t);
428 throw t;
429 } catch (Throwable t) {
430 threadUnexpectedException(t);
431 }
432 }
433
434
435
436
437
438
439 public void threadAssertSame(Object x, Object y) {
440 try {
441 assertSame(x, y);
442 } catch (AssertionFailedError t) {
443 threadRecordFailure(t);
444 throw t;
445 }
446 }
447
448
449
450
451 public void threadShouldThrow() {
452 threadFail("should throw exception");
453 }
454
455
456
457
458 public void threadShouldThrow(String exceptionName) {
459 threadFail("should throw " + exceptionName);
460 }
461
462
463
464
465
466
467 public void threadUnexpectedException(Throwable t) {
468 threadRecordFailure(t);
469 t.printStackTrace();
470 if (t instanceof RuntimeException)
471 throw (RuntimeException) t;
472 else if (t instanceof Error)
473 throw (Error) t;
474 else {
475 AssertionFailedError afe =
476 new AssertionFailedError("unexpected exception: " + t);
477 afe.initCause(t);
478 throw afe;
479 }
480 }
481
482
483
484
485
486
487 static void delay(long millis) throws InterruptedException {
488 long startTime = System.nanoTime();
489 long ns = millis * 1000 * 1000;
490 for (;;) {
491 if (millis > 0L)
492 Thread.sleep(millis);
493 else
494 Thread.yield();
495 long d = ns - (System.nanoTime() - startTime);
496 if (d > 0L)
497 millis = d / (1000 * 1000);
498 else
499 break;
500 }
501 }
502
503
504
505
506 void joinPool(ExecutorService exec) {
507 try {
508 exec.shutdown();
509 assertTrue("ExecutorService did not terminate in a timely manner",
510 exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
511 } catch (SecurityException ok) {
512
513 } catch (InterruptedException ie) {
514 fail("Unexpected InterruptedException");
515 }
516 }
517
518
519
520
521
522 void assertThreadStaysAlive(Thread thread) {
523 assertThreadStaysAlive(thread, timeoutMillis());
524 }
525
526
527
528
529 void assertThreadStaysAlive(Thread thread, long millis) {
530 try {
531
532 delay(millis);
533 assertTrue(thread.isAlive());
534 } catch (InterruptedException ie) {
535 fail("Unexpected InterruptedException");
536 }
537 }
538
539
540
541
542
543 void assertThreadsStayAlive(Thread... threads) {
544 assertThreadsStayAlive(timeoutMillis(), threads);
545 }
546
547
548
549
550 void assertThreadsStayAlive(long millis, Thread... threads) {
551 try {
552
553 delay(millis);
554 for (Thread thread : threads)
555 assertTrue(thread.isAlive());
556 } catch (InterruptedException ie) {
557 fail("Unexpected InterruptedException");
558 }
559 }
560
561
562
563
564
565 void assertFutureTimesOut(Future future) {
566 assertFutureTimesOut(future, timeoutMillis());
567 }
568
569
570
571
572 void assertFutureTimesOut(Future future, long timeoutMillis) {
573 long startTime = System.nanoTime();
574 try {
575 future.get(timeoutMillis, MILLISECONDS);
576 shouldThrow();
577 } catch (TimeoutException success) {
578 } catch (Exception e) {
579 threadUnexpectedException(e);
580 } finally { future.cancel(true); }
581 assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
582 }
583
584
585
586
587 public void shouldThrow() {
588 fail("Should throw exception");
589 }
590
591
592
593
594 public void shouldThrow(String exceptionName) {
595 fail("Should throw " + exceptionName);
596 }
597
598
599
600
601 public static final int SIZE = 20;
602
603
604
605 public static final Integer zero = new Integer(0);
606 public static final Integer one = new Integer(1);
607 public static final Integer two = new Integer(2);
608 public static final Integer three = new Integer(3);
609 public static final Integer four = new Integer(4);
610 public static final Integer five = new Integer(5);
611 public static final Integer six = new Integer(6);
612 public static final Integer seven = new Integer(7);
613 public static final Integer eight = new Integer(8);
614 public static final Integer nine = new Integer(9);
615 public static final Integer m1 = new Integer(-1);
616 public static final Integer m2 = new Integer(-2);
617 public static final Integer m3 = new Integer(-3);
618 public static final Integer m4 = new Integer(-4);
619 public static final Integer m5 = new Integer(-5);
620 public static final Integer m6 = new Integer(-6);
621 public static final Integer m10 = new Integer(-10);
622
623
624
625
626
627
628
629
630 public void runWithPermissions(Runnable r, Permission... permissions) {
631 SecurityManager sm = System.getSecurityManager();
632 if (sm == null) {
633 r.run();
634 Policy savedPolicy = Policy.getPolicy();
635 try {
636 Policy.setPolicy(permissivePolicy());
637 System.setSecurityManager(new SecurityManager());
638 runWithPermissions(r, permissions);
639 } finally {
640 System.setSecurityManager(null);
641 Policy.setPolicy(savedPolicy);
642 }
643 } else {
644 Policy savedPolicy = Policy.getPolicy();
645 AdjustablePolicy policy = new AdjustablePolicy(permissions);
646 Policy.setPolicy(policy);
647
648 try {
649 r.run();
650 } finally {
651 policy.addPermission(new SecurityPermission("setPolicy"));
652 Policy.setPolicy(savedPolicy);
653 }
654 }
655 }
656
657
658
659
660 public void runWithoutPermissions(Runnable r) {
661 runWithPermissions(r);
662 }
663
664
665
666
667
668 public static class AdjustablePolicy extends java.security.Policy {
669 Permissions perms = new Permissions();
670 AdjustablePolicy(Permission... permissions) {
671 for (Permission permission : permissions)
672 perms.add(permission);
673 }
674 void addPermission(Permission perm) { perms.add(perm); }
675 void clearPermissions() { perms = new Permissions(); }
676 public PermissionCollection getPermissions(CodeSource cs) {
677 return perms;
678 }
679 public PermissionCollection getPermissions(ProtectionDomain pd) {
680 return perms;
681 }
682 public boolean implies(ProtectionDomain pd, Permission p) {
683 return perms.implies(p);
684 }
685 public void refresh() {}
686 }
687
688
689
690
691 public static Policy permissivePolicy() {
692 return new AdjustablePolicy
693
694 (new RuntimePermission("modifyThread"),
695 new RuntimePermission("getClassLoader"),
696 new RuntimePermission("setContextClassLoader"),
697
698 new SecurityPermission("getPolicy"),
699 new SecurityPermission("setPolicy"),
700 new RuntimePermission("setSecurityManager"),
701
702 new RuntimePermission("accessDeclaredMembers"),
703 new PropertyPermission("*", "read"),
704 new java.io.FilePermission("<<ALL FILES>>", "read"));
705 }
706
707
708
709
710
711 void sleep(long millis) {
712 try {
713 delay(millis);
714 } catch (InterruptedException ie) {
715 AssertionFailedError afe =
716 new AssertionFailedError("Unexpected InterruptedException");
717 afe.initCause(ie);
718 throw afe;
719 }
720 }
721
722
723
724
725
726 void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
727 long startTime = System.nanoTime();
728 for (;;) {
729 Thread.State s = thread.getState();
730 if (s == Thread.State.BLOCKED ||
731 s == Thread.State.WAITING ||
732 s == Thread.State.TIMED_WAITING)
733 return;
734 else if (s == Thread.State.TERMINATED)
735 fail("Unexpected thread termination");
736 else if (millisElapsedSince(startTime) > timeoutMillis) {
737 threadAssertTrue(thread.isAlive());
738 return;
739 }
740 Thread.yield();
741 }
742 }
743
744
745
746
747
748 void waitForThreadToEnterWaitState(Thread thread) {
749 waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
750 }
751
752
753
754
755
756
757 long millisElapsedSince(long startNanoTime) {
758 return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
759 }
760
761
762
763
764 Thread newStartedThread(Runnable runnable) {
765 Thread t = new Thread(runnable);
766 t.setDaemon(true);
767 t.start();
768 return t;
769 }
770
771
772
773
774
775
776 void awaitTermination(Thread t, long timeoutMillis) {
777 try {
778 t.join(timeoutMillis);
779 } catch (InterruptedException ie) {
780 threadUnexpectedException(ie);
781 } finally {
782 if (t.getState() != Thread.State.TERMINATED) {
783 t.interrupt();
784 fail("Test timed out");
785 }
786 }
787 }
788
789
790
791
792
793
794 void awaitTermination(Thread t) {
795 awaitTermination(t, LONG_DELAY_MS);
796 }
797
798
799
800 public abstract class CheckedRunnable implements Runnable {
801 protected abstract void realRun() throws Throwable;
802
803 public final void run() {
804 try {
805 realRun();
806 } catch (Throwable t) {
807 threadUnexpectedException(t);
808 }
809 }
810 }
811
812 public abstract class RunnableShouldThrow implements Runnable {
813 protected abstract void realRun() throws Throwable;
814
815 final Class<?> exceptionClass;
816
817 <T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) {
818 this.exceptionClass = exceptionClass;
819 }
820
821 public final void run() {
822 try {
823 realRun();
824 threadShouldThrow(exceptionClass.getSimpleName());
825 } catch (Throwable t) {
826 if (! exceptionClass.isInstance(t))
827 threadUnexpectedException(t);
828 }
829 }
830 }
831
832 public abstract class ThreadShouldThrow extends Thread {
833 protected abstract void realRun() throws Throwable;
834
835 final Class<?> exceptionClass;
836
837 <T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) {
838 this.exceptionClass = exceptionClass;
839 }
840
841 public final void run() {
842 try {
843 realRun();
844 threadShouldThrow(exceptionClass.getSimpleName());
845 } catch (Throwable t) {
846 if (! exceptionClass.isInstance(t))
847 threadUnexpectedException(t);
848 }
849 }
850 }
851
852 public abstract class CheckedInterruptedRunnable implements Runnable {
853 protected abstract void realRun() throws Throwable;
854
855 public final void run() {
856 try {
857 realRun();
858 threadShouldThrow("InterruptedException");
859 } catch (InterruptedException success) {
860 threadAssertFalse(Thread.interrupted());
861 } catch (Throwable t) {
862 threadUnexpectedException(t);
863 }
864 }
865 }
866
867 public abstract class CheckedCallable<T> implements Callable<T> {
868 protected abstract T realCall() throws Throwable;
869
870 public final T call() {
871 try {
872 return realCall();
873 } catch (Throwable t) {
874 threadUnexpectedException(t);
875 return null;
876 }
877 }
878 }
879
880 public abstract class CheckedInterruptedCallable<T>
881 implements Callable<T> {
882 protected abstract T realCall() throws Throwable;
883
884 public final T call() {
885 try {
886 T result = realCall();
887 threadShouldThrow("InterruptedException");
888 return result;
889 } catch (InterruptedException success) {
890 threadAssertFalse(Thread.interrupted());
891 } catch (Throwable t) {
892 threadUnexpectedException(t);
893 }
894 return null;
895 }
896 }
897
898 public static class NoOpRunnable implements Runnable {
899 public void run() {}
900 }
901
902 public static class NoOpCallable implements Callable {
903 public Object call() { return Boolean.TRUE; }
904 }
905
906 public static final String TEST_STRING = "a test string";
907
908 public static class StringTask implements Callable<String> {
909 public String call() { return TEST_STRING; }
910 }
911
912 public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
913 return new CheckedCallable<String>() {
914 protected String realCall() {
915 try {
916 latch.await();
917 } catch (InterruptedException quittingTime) {}
918 return TEST_STRING;
919 }};
920 }
921
922 public Runnable awaiter(final CountDownLatch latch) {
923 return new CheckedRunnable() {
924 public void realRun() throws InterruptedException {
925 await(latch);
926 }};
927 }
928
929 public void await(CountDownLatch latch) {
930 try {
931 assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
932 } catch (Throwable t) {
933 threadUnexpectedException(t);
934 }
935 }
936
937 public void await(Semaphore semaphore) {
938 try {
939 assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
940 } catch (Throwable t) {
941 threadUnexpectedException(t);
942 }
943 }
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964 public static class NPETask implements Callable<String> {
965 public String call() { throw new NullPointerException(); }
966 }
967
968 public static class CallableOne implements Callable<Integer> {
969 public Integer call() { return one; }
970 }
971
972 public class ShortRunnable extends CheckedRunnable {
973 protected void realRun() throws Throwable {
974 delay(SHORT_DELAY_MS);
975 }
976 }
977
978 public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
979 protected void realRun() throws InterruptedException {
980 delay(SHORT_DELAY_MS);
981 }
982 }
983
984 public class SmallRunnable extends CheckedRunnable {
985 protected void realRun() throws Throwable {
986 delay(SMALL_DELAY_MS);
987 }
988 }
989
990 public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
991 protected void realRun() {
992 try {
993 delay(SMALL_DELAY_MS);
994 } catch (InterruptedException ok) {}
995 }
996 }
997
998 public class SmallCallable extends CheckedCallable {
999 protected Object realCall() throws InterruptedException {
1000 delay(SMALL_DELAY_MS);
1001 return Boolean.TRUE;
1002 }
1003 }
1004
1005 public class MediumRunnable extends CheckedRunnable {
1006 protected void realRun() throws Throwable {
1007 delay(MEDIUM_DELAY_MS);
1008 }
1009 }
1010
1011 public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
1012 protected void realRun() throws InterruptedException {
1013 delay(MEDIUM_DELAY_MS);
1014 }
1015 }
1016
1017 public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
1018 return new CheckedRunnable() {
1019 protected void realRun() {
1020 try {
1021 delay(timeoutMillis);
1022 } catch (InterruptedException ok) {}
1023 }};
1024 }
1025
1026 public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
1027 protected void realRun() {
1028 try {
1029 delay(MEDIUM_DELAY_MS);
1030 } catch (InterruptedException ok) {}
1031 }
1032 }
1033
1034 public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
1035 protected void realRun() {
1036 try {
1037 delay(LONG_DELAY_MS);
1038 } catch (InterruptedException ok) {}
1039 }
1040 }
1041
1042
1043
1044
1045 public static class SimpleThreadFactory implements ThreadFactory {
1046 public Thread newThread(Runnable r) {
1047 return new Thread(r);
1048 }
1049 }
1050
1051 public interface TrackedRunnable extends Runnable {
1052 boolean isDone();
1053 }
1054
1055 public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
1056 return new TrackedRunnable() {
1057 private volatile boolean done = false;
1058 public boolean isDone() { return done; }
1059 public void run() {
1060 try {
1061 delay(timeoutMillis);
1062 done = true;
1063 } catch (InterruptedException ok) {}
1064 }
1065 };
1066 }
1067
1068 public static class TrackedShortRunnable implements Runnable {
1069 public volatile boolean done = false;
1070 public void run() {
1071 try {
1072 delay(SHORT_DELAY_MS);
1073 done = true;
1074 } catch (InterruptedException ok) {}
1075 }
1076 }
1077
1078 public static class TrackedSmallRunnable implements Runnable {
1079 public volatile boolean done = false;
1080 public void run() {
1081 try {
1082 delay(SMALL_DELAY_MS);
1083 done = true;
1084 } catch (InterruptedException ok) {}
1085 }
1086 }
1087
1088 public static class TrackedMediumRunnable implements Runnable {
1089 public volatile boolean done = false;
1090 public void run() {
1091 try {
1092 delay(MEDIUM_DELAY_MS);
1093 done = true;
1094 } catch (InterruptedException ok) {}
1095 }
1096 }
1097
1098 public static class TrackedLongRunnable implements Runnable {
1099 public volatile boolean done = false;
1100 public void run() {
1101 try {
1102 delay(LONG_DELAY_MS);
1103 done = true;
1104 } catch (InterruptedException ok) {}
1105 }
1106 }
1107
1108 public static class TrackedNoOpRunnable implements Runnable {
1109 public volatile boolean done = false;
1110 public void run() {
1111 done = true;
1112 }
1113 }
1114
1115 public static class TrackedCallable implements Callable {
1116 public volatile boolean done = false;
1117 public Object call() {
1118 try {
1119 delay(SMALL_DELAY_MS);
1120 done = true;
1121 } catch (InterruptedException ok) {}
1122 return Boolean.TRUE;
1123 }
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 public static class NoOpREHandler implements RejectedExecutionHandler {
1161 public void rejectedExecution(Runnable r,
1162 ThreadPoolExecutor executor) {}
1163 }
1164
1165
1166
1167
1168
1169 public class CheckedBarrier extends CyclicBarrier {
1170 public CheckedBarrier(int parties) { super(parties); }
1171
1172 public int await() {
1173 try {
1174 return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1175 } catch (TimeoutException e) {
1176 throw new AssertionFailedError("timed out");
1177 } catch (Exception e) {
1178 AssertionFailedError afe =
1179 new AssertionFailedError("Unexpected exception: " + e);
1180 afe.initCause(e);
1181 throw afe;
1182 }
1183 }
1184 }
1185
1186 void checkEmpty(BlockingQueue q) {
1187 try {
1188 assertTrue(q.isEmpty());
1189 assertEquals(0, q.size());
1190 assertNull(q.peek());
1191 assertNull(q.poll());
1192 assertNull(q.poll(0, MILLISECONDS));
1193 assertEquals(q.toString(), "[]");
1194 assertTrue(Arrays.equals(q.toArray(), new Object[0]));
1195 assertFalse(q.iterator().hasNext());
1196 try {
1197 q.element();
1198 shouldThrow();
1199 } catch (NoSuchElementException success) {}
1200 try {
1201 q.iterator().next();
1202 shouldThrow();
1203 } catch (NoSuchElementException success) {}
1204 try {
1205 q.remove();
1206 shouldThrow();
1207 } catch (NoSuchElementException success) {}
1208 } catch (InterruptedException ie) {
1209 threadUnexpectedException(ie);
1210 }
1211 }
1212
1213 @SuppressWarnings("unchecked")
1214 <T> T serialClone(T o) {
1215 try {
1216 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1217 ObjectOutputStream oos = new ObjectOutputStream(bos);
1218 oos.writeObject(o);
1219 oos.flush();
1220 oos.close();
1221 ObjectInputStream ois = new ObjectInputStream
1222 (new ByteArrayInputStream(bos.toByteArray()));
1223 T clone = (T) ois.readObject();
1224 assertSame(o.getClass(), clone.getClass());
1225 return clone;
1226 } catch (Throwable t) {
1227 threadUnexpectedException(t);
1228 return null;
1229 }
1230 }
1231 }