diff options
Diffstat (limited to 'ui-client/src/test/java/market/guess/ui')
| -rw-r--r-- | ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java index 5c1057f..e3b11f0 100644 --- a/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java +++ b/ui-client/src/test/java/market/guess/ui/client/RemoteContextsTest.java | |||
| @@ -6,7 +6,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; | |||
| 6 | 6 | ||
| 7 | import java.math.BigDecimal; | 7 | import java.math.BigDecimal; |
| 8 | import java.nio.file.Path; | 8 | import java.nio.file.Path; |
| 9 | import java.util.ArrayList; | ||
| 10 | import java.util.List; | ||
| 9 | import java.util.concurrent.CountDownLatch; | 11 | import java.util.concurrent.CountDownLatch; |
| 12 | import java.util.concurrent.Executors; | ||
| 13 | import java.util.concurrent.Future; | ||
| 10 | import java.util.concurrent.TimeUnit; | 14 | import java.util.concurrent.TimeUnit; |
| 11 | import market.guess.model.event.EventStatus; | 15 | import market.guess.model.event.EventStatus; |
| 12 | import market.guess.server.TestServer; | 16 | import market.guess.server.TestServer; |
| @@ -84,6 +88,68 @@ class RemoteContextsTest { | |||
| 84 | } | 88 | } |
| 85 | 89 | ||
| 86 | @Test | 90 | @Test |
| 91 | void aWholeEventLifecycleConservesMoney() throws Exception { | ||
| 92 | var traders = List.of("Uri", "Vered", "Wim", "Xena"); | ||
| 93 | var clients = new ArrayList<ServerClient>(); | ||
| 94 | try (var ora = new ServerClient(server.api)) { | ||
| 95 | ora.login("Ora"); | ||
| 96 | var catalog = new RemoteCatalogContext(ora); | ||
| 97 | var loaded = catalog.loadEvents(Path.of("test-data/ex-3/multiple.xml")); | ||
| 98 | assertEquals(3, loaded.getData().eventsLoaded()); | ||
| 99 | var key = | ||
| 100 | catalog.getAllEvents().getData().stream() | ||
| 101 | .filter(e -> e.name().equals("Will it rain tomorrow ?")) | ||
| 102 | .findFirst() | ||
| 103 | .orElseThrow() | ||
| 104 | .key(); | ||
| 105 | new RemoteAccountContext(ora).deposit("Ora", "1000"); | ||
| 106 | assertTrue(catalog.openEvent(key).isSuccess()); | ||
| 107 | |||
| 108 | for (var name : traders) { | ||
| 109 | var c = new ServerClient(server.api); | ||
| 110 | clients.add(c); | ||
| 111 | c.login(name); | ||
| 112 | new RemoteAccountContext(c).deposit(name, "100"); | ||
| 113 | } | ||
| 114 | var pool = Executors.newFixedThreadPool(traders.size()); | ||
| 115 | var orders = new ArrayList<Future<Boolean>>(); | ||
| 116 | for (var c : clients) { | ||
| 117 | orders.add( | ||
| 118 | pool.submit( | ||
| 119 | () -> | ||
| 120 | new RemoteGuessMarketContext(c) | ||
| 121 | .placeOrder("", key, key + ":0", "BUY", BigDecimal.ONE, 5) | ||
| 122 | .isSuccess())); | ||
| 123 | } | ||
| 124 | for (var order : orders) { | ||
| 125 | assertTrue(order.get(10, TimeUnit.SECONDS)); | ||
| 126 | } | ||
| 127 | pool.shutdown(); | ||
| 128 | |||
| 129 | var beforePayout = balance(clients.get(0), "Uri"); | ||
| 130 | var settled = new RemoteGuessMarketContext(ora).settleEvent("Ora", key, key + ":0"); | ||
| 131 | assertTrue(settled.isSuccess(), settled.getMessage()); | ||
| 132 | assertEquals(EventStatus.SETTLED, catalog.getEvent(key).getData().summary().status()); | ||
| 133 | assertEquals("0.00", catalog.getEvent(key).getData().summary().accountBalance()); | ||
| 134 | |||
| 135 | assertEquals(new BigDecimal("4.50"), balance(clients.get(0), "Uri").subtract(beforePayout)); | ||
| 136 | |||
| 137 | var total = balance(ora, "Ora"); | ||
| 138 | for (var i = 0; i < traders.size(); i++) { | ||
| 139 | total = total.add(balance(clients.get(i), traders.get(i))); | ||
| 140 | } | ||
| 141 | assertEquals(new BigDecimal("1400.00"), total); | ||
| 142 | } finally { | ||
| 143 | clients.forEach(ServerClient::close); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | private static BigDecimal balance(ServerClient client, String name) { | ||
| 148 | client.pull(); | ||
| 149 | return new BigDecimal(new RemoteAccountContext(client).getAccount(name).getData().balance()); | ||
| 150 | } | ||
| 151 | |||
| 152 | @Test | ||
| 87 | void anUnreachableServerIsAFailedResultNotAnException() { | 153 | void anUnreachableServerIsAFailedResultNotAnException() { |
| 88 | try (var nobody = new ServerClient("http://localhost:1/api")) { | 154 | try (var nobody = new ServerClient("http://localhost:1/api")) { |
| 89 | assertFalse(nobody.login("Zed").isSuccess()); | 155 | assertFalse(nobody.login("Zed").isSuccess()); |