|
| 1 | +package com.shadowcs.themis.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import java.util.concurrent.atomic.AtomicInteger; |
| 6 | +import org.junit.Test; |
| 7 | +import com.shadowcs.themis.util.concurrent.ProvidedThreadFactory; |
| 8 | + |
| 9 | +public class ProvidedThreadFactoryTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void objectCreation() { |
| 13 | + new ProvidedThreadFactory().close(); |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + public void threadCreationBlank() throws InterruptedException { |
| 18 | + |
| 19 | + ProvidedThreadFactory pThreadFactory = new ProvidedThreadFactory(); |
| 20 | + new Thread(() -> pThreadFactory.cede()).start(); |
| 21 | + Thread.sleep(1000); |
| 22 | + for(int i = 0; i < 100; i++) { |
| 23 | + Thread t = pThreadFactory.newThread(null); |
| 24 | + if(t != null) { |
| 25 | + t.start(); |
| 26 | + t.join(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + pThreadFactory.close(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void threadCreationCede() throws InterruptedException { |
| 35 | + |
| 36 | + ProvidedThreadFactory pThreadFactory = new ProvidedThreadFactory(); |
| 37 | + new Thread(() -> pThreadFactory.cede(() -> System.out.println("Test"))).start(); // TODO: replace this with a boolean or something |
| 38 | + Thread.sleep(500); // sleeps are bad for tests but I can't think of a better way atm |
| 39 | + for(int i = 0; i < 100; i++) { |
| 40 | + Thread t = pThreadFactory.newThread(null); |
| 41 | + if(t != null) { |
| 42 | + t.start(); |
| 43 | + t.join(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + Thread.sleep(500); |
| 48 | + pThreadFactory.close(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void threadWorkSingle() throws InterruptedException { |
| 53 | + |
| 54 | + AtomicInteger expected = new AtomicInteger(); |
| 55 | + AtomicInteger integer = new AtomicInteger(); |
| 56 | + |
| 57 | + ProvidedThreadFactory pThreadFactory = new ProvidedThreadFactory(); |
| 58 | + Thread thread = new Thread(() -> pThreadFactory.cede()); |
| 59 | + thread.start(); |
| 60 | + |
| 61 | + Thread.sleep(500); // TODO: this one is an even worse test |
| 62 | + |
| 63 | + for(int i = 0; i < 100; i++) { |
| 64 | + Thread t = pThreadFactory.newThread(() -> integer.incrementAndGet()); |
| 65 | + if(t != null) { |
| 66 | + expected.incrementAndGet(); |
| 67 | + t.start(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Thread.sleep(500); |
| 72 | + pThreadFactory.close(); |
| 73 | + thread.join(); |
| 74 | + |
| 75 | + System.out.println(expected.get()); |
| 76 | + assertEquals(expected.get(), integer.get()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void threadWorkInterrupt() throws InterruptedException { |
| 81 | + |
| 82 | + ProvidedThreadFactory pThreadFactory = new ProvidedThreadFactory(); |
| 83 | + Thread thread = new Thread(() -> pThreadFactory.cede()); |
| 84 | + thread.start(); |
| 85 | + |
| 86 | + Thread.sleep(500); // TODO: this one is an even worse test |
| 87 | + |
| 88 | + for(int i = 0; i < 100; i++) { |
| 89 | + Thread t = pThreadFactory.newThread(() -> { |
| 90 | + |
| 91 | + Thread.currentThread().interrupt(); |
| 92 | + }); |
| 93 | + |
| 94 | + if(t != null) { |
| 95 | + t.start(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + thread.join(5000); |
| 100 | + pThreadFactory.close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void threadWorkMulti() throws InterruptedException { |
| 105 | + |
| 106 | + AtomicInteger expected = new AtomicInteger(); |
| 107 | + AtomicInteger integer = new AtomicInteger(); |
| 108 | + |
| 109 | + ProvidedThreadFactory pThreadFactory = new ProvidedThreadFactory(); |
| 110 | + Thread thread0 = new Thread(() -> pThreadFactory.cede()); |
| 111 | + Thread thread1 = new Thread(() -> pThreadFactory.cede()); |
| 112 | + Thread thread2 = new Thread(() -> pThreadFactory.cede()); |
| 113 | + Thread thread3 = new Thread(() -> pThreadFactory.cede()); |
| 114 | + Thread thread4 = new Thread(() -> pThreadFactory.cede()); |
| 115 | + Thread thread5 = new Thread(() -> pThreadFactory.cede()); |
| 116 | + Thread thread6 = new Thread(() -> pThreadFactory.cede()); |
| 117 | + Thread thread7 = new Thread(() -> pThreadFactory.cede()); |
| 118 | + Thread thread8 = new Thread(() -> pThreadFactory.cede()); |
| 119 | + Thread thread9 = new Thread(() -> pThreadFactory.cede()); |
| 120 | + thread0.start(); |
| 121 | + thread1.start(); |
| 122 | + thread2.start(); |
| 123 | + thread3.start(); |
| 124 | + thread4.start(); |
| 125 | + thread5.start(); |
| 126 | + thread6.start(); |
| 127 | + thread7.start(); |
| 128 | + thread8.start(); |
| 129 | + thread9.start(); |
| 130 | + |
| 131 | + for(int i = 0; i < 100; i++) { |
| 132 | + Thread t = pThreadFactory.newThread(() -> integer.incrementAndGet()); |
| 133 | + if(t != null) { |
| 134 | + expected.incrementAndGet(); |
| 135 | + t.start(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + pThreadFactory.close(); |
| 140 | + thread0.join(); |
| 141 | + thread1.join(); |
| 142 | + thread2.join(); |
| 143 | + thread3.join(); |
| 144 | + thread4.join(); |
| 145 | + thread5.join(); |
| 146 | + thread6.join(); |
| 147 | + thread7.join(); |
| 148 | + thread8.join(); |
| 149 | + thread9.join(); // need to find a better way than this |
| 150 | + |
| 151 | + System.out.println(expected.get()); |
| 152 | + assertTrue(expected.get() > 0); // TODO: need a better way than this |
| 153 | + assertEquals(expected.get(), integer.get()); |
| 154 | + } |
| 155 | +} |
0 commit comments