|
| 1 | +package com.vaadin.flow.uitest.ui.faulttolerance; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.UncheckedIOException; |
| 5 | +import java.nio.file.FileSystems; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.StandardWatchEventKinds; |
| 9 | +import java.nio.file.WatchEvent; |
| 10 | +import java.nio.file.WatchKey; |
| 11 | +import java.nio.file.WatchService; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 14 | + |
| 15 | +import org.junit.After; |
| 16 | +import org.junit.Rule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.rules.TemporaryFolder; |
| 19 | +import org.openqa.selenium.By; |
| 20 | + |
| 21 | +import com.vaadin.flow.component.html.testbench.NativeButtonElement; |
| 22 | +import com.vaadin.flow.component.html.testbench.SpanElement; |
| 23 | +import com.vaadin.flow.testutil.ChromeBrowserTestWithProxy; |
| 24 | + |
| 25 | +public class NetworkInterruptionIT extends ChromeBrowserTestWithProxy { |
| 26 | + |
| 27 | + private AtomicBoolean stopWatcher = new AtomicBoolean(false); |
| 28 | + |
| 29 | + @Rule |
| 30 | + public TemporaryFolder tempDir = new TemporaryFolder(); |
| 31 | + |
| 32 | + @Override |
| 33 | + public void setup() throws Exception { |
| 34 | + super.setup(); |
| 35 | + Path proxyMonitorFile = tempDir.newFile("flow-test-proxy-monitor.txt") |
| 36 | + .toPath(); |
| 37 | + WatchService watchService = FileSystems.getDefault().newWatchService(); |
| 38 | + tempDir.getRoot().toPath().register(watchService, |
| 39 | + StandardWatchEventKinds.ENTRY_MODIFY); |
| 40 | + AtomicBoolean stopWatcher = new AtomicBoolean(false); |
| 41 | + new Thread(() -> { |
| 42 | + WatchKey key; |
| 43 | + try (WatchService ws = watchService) { |
| 44 | + while (!stopWatcher.get()) { |
| 45 | + key = ws.poll(100, TimeUnit.MILLISECONDS); |
| 46 | + if (key != null) { |
| 47 | + for (WatchEvent<?> event : key.pollEvents()) { |
| 48 | + if (event.context() instanceof Path p |
| 49 | + && proxyMonitorFile.equals(tempDir.getRoot() |
| 50 | + .toPath().resolve(p))) { |
| 51 | + if (Files.readString(proxyMonitorFile) |
| 52 | + .contains("stop")) { |
| 53 | + disconnectProxy(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + key.reset(); |
| 58 | + } |
| 59 | + } |
| 60 | + } catch (IOException e) { |
| 61 | + throw new UncheckedIOException(e); |
| 62 | + } catch (InterruptedException e) { |
| 63 | + Thread.currentThread().interrupt(); |
| 64 | + throw new IllegalStateException(e); |
| 65 | + } |
| 66 | + }).start(); |
| 67 | + this.stopWatcher = stopWatcher; |
| 68 | + open("proxyMonitorFile=" + proxyMonitorFile.toAbsolutePath()); |
| 69 | + testBench().disableWaitForVaadin(); |
| 70 | + } |
| 71 | + |
| 72 | + @After |
| 73 | + public void stopWatcher() { |
| 74 | + stopWatcher.set(true); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void networkInterruption_clickIncrementButton_messageQueuedAndResent() |
| 79 | + throws IOException { |
| 80 | + disconnectProxy(); |
| 81 | + |
| 82 | + $(NativeButtonElement.class) |
| 83 | + .id(NetworkInterruptionView.INCREMENT_BUTTON_ID).click(); |
| 84 | + waitForReconnectAttempts(); |
| 85 | + connectProxy(); |
| 86 | + |
| 87 | + waitForLogMessage("Re-established connection to server"); |
| 88 | + |
| 89 | + waitUntil(d -> Integer.parseInt($(SpanElement.class) |
| 90 | + .id(NetworkInterruptionView.COUNTER_ID).getText()) == 1); |
| 91 | + ensureNoSystemErrorFromServer(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void networkInterruption_clickIncrementButtonMultipleTime_messagesQueuedAndResent() |
| 96 | + throws IOException { |
| 97 | + disconnectProxy(); |
| 98 | + |
| 99 | + NativeButtonElement button = $(NativeButtonElement.class) |
| 100 | + .id(NetworkInterruptionView.INCREMENT_BUTTON_ID); |
| 101 | + |
| 102 | + button.click(); |
| 103 | + button.click(); |
| 104 | + button.click(); |
| 105 | + button.click(); |
| 106 | + waitForReconnectAttempts(); |
| 107 | + connectProxy(); |
| 108 | + |
| 109 | + waitForLogMessage("Re-established connection to server"); |
| 110 | + |
| 111 | + waitUntil(d -> Integer.parseInt($(SpanElement.class) |
| 112 | + .id(NetworkInterruptionView.COUNTER_ID).getText()) == 4); |
| 113 | + ensureNoSystemErrorFromServer(); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void networkInterruption_dropProxyBeforeResponse_serverMessageCachedAndResent() |
| 118 | + throws Exception { |
| 119 | + $(NativeButtonElement.class) |
| 120 | + .id(NetworkInterruptionView.INCREMENT_STOP_PROXY_BUTTON_ID) |
| 121 | + .click(); |
| 122 | + waitForReconnectAttempts(); |
| 123 | + connectProxy(); |
| 124 | + waitForLogMessage("Re-established connection to server"); |
| 125 | + |
| 126 | + waitUntil(d -> Integer.parseInt($(SpanElement.class) |
| 127 | + .id(NetworkInterruptionView.COUNTER_ID).getText()) == 1); |
| 128 | + ensureNoSystemErrorFromServer(); |
| 129 | + } |
| 130 | + |
| 131 | + private void waitForReconnectAttempts() { |
| 132 | + waitForLogMessage("Reconnect attempt 2 for XHR"); |
| 133 | + } |
| 134 | + |
| 135 | + private void ensureNoSystemErrorFromServer() { |
| 136 | + // Make sure there is no error caused by messages sync lost |
| 137 | + waitForElementNotPresent(By.cssSelector("div.v-system-error")); |
| 138 | + } |
| 139 | + |
| 140 | + private void waitForLogMessage(String expectedMessage) { |
| 141 | + waitUntil(driver -> getBrowserLogs(true).stream().anyMatch( |
| 142 | + message -> expectedMessage.equals(message.toString()))); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments