|
| 1 | +package io.appium.java_client.localserver; |
| 2 | + |
| 3 | + |
| 4 | +import io.appium.java_client.service.local.AppiumDriverLocalService; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +public class ThreadSafetyTest { |
| 10 | + |
| 11 | + private static abstract class Action implements Cloneable { |
| 12 | + abstract Object perform(); |
| 13 | + |
| 14 | + public Action clone() { |
| 15 | + try { |
| 16 | + return (Action) super.clone(); |
| 17 | + } |
| 18 | + catch (Throwable t) { |
| 19 | + throw new RuntimeException(t); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + private static class TestThread implements Runnable { |
| 25 | + private final Action action; |
| 26 | + private Object result; |
| 27 | + private Throwable t; |
| 28 | + |
| 29 | + TestThread(Action action) { |
| 30 | + this.action = action; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void run() { |
| 35 | + try { |
| 36 | + result = action.perform(); |
| 37 | + } |
| 38 | + catch (Throwable t) { |
| 39 | + this.t = t; |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + final AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService(); |
| 46 | + final Action run = new Action() { |
| 47 | + @Override |
| 48 | + Object perform() { |
| 49 | + service.start(); |
| 50 | + return "OK"; |
| 51 | + } |
| 52 | + }; |
| 53 | + final Action run2 = run.clone(); |
| 54 | + |
| 55 | + final Action isRunning = new Action() { |
| 56 | + @Override |
| 57 | + Object perform() { |
| 58 | + return service.isRunning(); |
| 59 | + } |
| 60 | + }; |
| 61 | + final Action isRunning2 = isRunning.clone(); |
| 62 | + |
| 63 | + final Action stop = new Action() { |
| 64 | + @Override |
| 65 | + Object perform() { |
| 66 | + service.stop(); |
| 67 | + return "OK"; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + final Action stop2 = stop.clone(); |
| 72 | + |
| 73 | + @Test |
| 74 | + public void whenFewTreadsDoTheSameWork() throws Throwable { |
| 75 | + |
| 76 | + TestThread runTestThread = new TestThread(run); |
| 77 | + TestThread runTestThread2 = new TestThread(run2); |
| 78 | + |
| 79 | + TestThread isRunningTestThread = new TestThread(isRunning); |
| 80 | + TestThread isRunningTestThread2 = new TestThread(isRunning2); |
| 81 | + |
| 82 | + TestThread stopTestThread = new TestThread(stop); |
| 83 | + TestThread stopTestThread2 = new TestThread(stop2); |
| 84 | + |
| 85 | + Thread runThread = new Thread(runTestThread); |
| 86 | + Thread runThread2 = new Thread(runTestThread2); |
| 87 | + |
| 88 | + Thread isRunningThread = new Thread(isRunningTestThread); |
| 89 | + Thread isRunningThread2 = new Thread(isRunningTestThread2); |
| 90 | + |
| 91 | + Thread stopThread = new Thread(stopTestThread); |
| 92 | + Thread stopThread2 = new Thread(stopTestThread2); |
| 93 | + |
| 94 | + try { |
| 95 | + runThread.start(); |
| 96 | + runThread2.start(); |
| 97 | + |
| 98 | + while (runThread.isAlive() || runThread2.isAlive()) { |
| 99 | + //do nothing |
| 100 | + } |
| 101 | + |
| 102 | + if (runTestThread.t != null) { |
| 103 | + throw runTestThread.t; |
| 104 | + } |
| 105 | + |
| 106 | + if (runTestThread2.t != null) { |
| 107 | + throw runTestThread2.t; |
| 108 | + } |
| 109 | + |
| 110 | + assertTrue(runTestThread.result.equals("OK")); |
| 111 | + assertTrue(runTestThread2.result.equals("OK")); |
| 112 | + assertTrue(service.isRunning()); |
| 113 | + |
| 114 | + isRunningThread.start(); |
| 115 | + isRunningThread2.start(); |
| 116 | + |
| 117 | + while (isRunningThread.isAlive() || isRunningThread2.isAlive()) { |
| 118 | + //do nothing |
| 119 | + } |
| 120 | + |
| 121 | + if (isRunningTestThread.t != null) { |
| 122 | + throw isRunningTestThread.t; |
| 123 | + } |
| 124 | + |
| 125 | + if (isRunningTestThread2.t != null) { |
| 126 | + throw isRunningTestThread2.t; |
| 127 | + } |
| 128 | + |
| 129 | + assertTrue(isRunningTestThread.result.equals(true)); |
| 130 | + assertTrue(isRunningTestThread2.result.equals(true)); |
| 131 | + |
| 132 | + stopThread.start(); |
| 133 | + stopThread2.start(); |
| 134 | + |
| 135 | + while (stopThread.isAlive() || stopThread2.isAlive()) { |
| 136 | + //do nothing |
| 137 | + } |
| 138 | + |
| 139 | + if (stopTestThread.t != null) { |
| 140 | + throw stopTestThread.t; |
| 141 | + } |
| 142 | + |
| 143 | + if (stopTestThread2.t != null) { |
| 144 | + throw stopTestThread2.t; |
| 145 | + } |
| 146 | + |
| 147 | + assertTrue(stopTestThread.result.equals("OK")); |
| 148 | + assertTrue(stopTestThread2.result.equals("OK")); |
| 149 | + assertTrue(!service.isRunning()); |
| 150 | + } |
| 151 | + finally { |
| 152 | + if (service.isRunning()) { |
| 153 | + service.stop(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void whenFewTreadsDoDifferentWork() throws Throwable { |
| 161 | + TestThread runTestThread = new TestThread(run); |
| 162 | + TestThread runTestThread2 = new TestThread(run2); |
| 163 | + |
| 164 | + TestThread isRunningTestThread = new TestThread(isRunning); |
| 165 | + TestThread isRunningTestThread2 = new TestThread(isRunning2); |
| 166 | + |
| 167 | + TestThread stopTestThread = new TestThread(stop); |
| 168 | + TestThread stopTestThread2 = new TestThread(stop2); |
| 169 | + |
| 170 | + Thread runThread = new Thread(runTestThread); |
| 171 | + Thread runThread2 = new Thread(runTestThread2); |
| 172 | + |
| 173 | + Thread isRunningThread = new Thread(isRunningTestThread); |
| 174 | + Thread isRunningThread2 = new Thread(isRunningTestThread2); |
| 175 | + |
| 176 | + Thread stopThread = new Thread(stopTestThread); |
| 177 | + Thread stopThread2 = new Thread(stopTestThread2); |
| 178 | + |
| 179 | + try { |
| 180 | + runThread.start(); //(1) |
| 181 | + Thread.sleep(10); |
| 182 | + isRunningThread.start();//(2) |
| 183 | + Thread.sleep(10); |
| 184 | + stopThread.start(); //(3) |
| 185 | + |
| 186 | + while (runThread.isAlive() || isRunningThread.isAlive() || stopThread.isAlive()) { |
| 187 | + //do nothing |
| 188 | + } |
| 189 | + |
| 190 | + if (runTestThread.t != null) { |
| 191 | + throw runTestThread.t; |
| 192 | + } |
| 193 | + |
| 194 | + if (isRunningTestThread.t != null) { |
| 195 | + throw isRunningTestThread.t; |
| 196 | + } |
| 197 | + |
| 198 | + if (stopTestThread.t != null) { |
| 199 | + throw stopTestThread.t; |
| 200 | + } |
| 201 | + |
| 202 | + assertTrue(runTestThread.result.equals("OK")); //the service had been started firstly (see (1)) |
| 203 | + assertTrue(isRunningTestThread.result.equals(true)); //it was running (see (2)) |
| 204 | + assertTrue(stopTestThread.result.equals("OK")); //and then the test tried to shut down it (see (3)) |
| 205 | + assertTrue(!service.isRunning()); |
| 206 | + |
| 207 | + isRunningThread2.start(); // (1) |
| 208 | + Thread.sleep(10); |
| 209 | + stopThread2.start(); // (2) |
| 210 | + Thread.sleep(10); |
| 211 | + runThread2.start(); //(3) |
| 212 | + |
| 213 | + while (runThread2.isAlive() || isRunningThread2.isAlive() || stopThread2.isAlive()) { |
| 214 | + //do nothing |
| 215 | + } |
| 216 | + |
| 217 | + if (runTestThread2.t != null) { |
| 218 | + throw runTestThread.t; |
| 219 | + } |
| 220 | + |
| 221 | + if (isRunningTestThread2.t != null) { |
| 222 | + throw isRunningTestThread.t; |
| 223 | + } |
| 224 | + |
| 225 | + if (stopTestThread2.t != null) { |
| 226 | + throw stopTestThread.t; |
| 227 | + } |
| 228 | + |
| 229 | + assertTrue(isRunningTestThread2.result.equals(false)); //the service wasn't being running (see (1)) |
| 230 | + assertTrue(stopTestThread2.result.equals("OK")); //the service had not been started firstly (see (2)), it is ok |
| 231 | + assertTrue(runTestThread2.result.equals("OK")); //and then it was started (see (3)) |
| 232 | + assertTrue(service.isRunning()); |
| 233 | + } |
| 234 | + finally { |
| 235 | + if (service.isRunning()) { |
| 236 | + service.stop(); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments