-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZK-5707: avoid invoking a event listener after a session expired
- Loading branch information
1 parent
1d27a30
commit 2bbeca0
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
zktest/src/main/java/org/zkoss/zktest/test2/B101_ZK_5707Composer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* B101_ZK_5707Composer.java | ||
Purpose: | ||
Description: | ||
History: | ||
12:44 PM 2024/9/19, Created by jumperchen | ||
Copyright (C) 2024 Potix Corporation. All Rights Reserved. | ||
*/ | ||
package org.zkoss.zktest.test2; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.IntStream; | ||
|
||
import org.zkoss.zk.ui.Component; | ||
import org.zkoss.zk.ui.Sessions; | ||
import org.zkoss.zk.ui.event.Events; | ||
import org.zkoss.zk.ui.http.SimpleSession; | ||
import org.zkoss.zk.ui.util.Composer; | ||
import org.zkoss.zul.Button; | ||
|
||
/** | ||
* @author jumperchen | ||
*/ | ||
public class B101_ZK_5707Composer implements Composer { | ||
static int count = 0; | ||
@Override | ||
public void doAfterCompose(Component comp) throws Exception { | ||
Button button = new Button("many users logout"); | ||
button.addEventListener(Events.ON_CLICK, e -> { | ||
Events.postEvent("onLogout", comp, null); | ||
IntStream.range(0, 100).forEach(n -> Events.postEvent("onDummy", comp, null)); | ||
}); | ||
comp.addEventListener("onLogout", e -> { | ||
// HttpSession session = (HttpSession) Sessions.getCurrent().getNativeSession(); | ||
SimpleSession simpleSession = ((SimpleSession)Sessions.getCurrent()); | ||
// simulate session logout/timeout from a different thread (browser tab/background process) | ||
System.out.println(">> handle onLogout "); | ||
CompletableFuture.runAsync( () ->{ | ||
System.out.println(">> handle onLogout Now"); | ||
// session.invalidate(); | ||
simpleSession.invalidateNow(); | ||
}); | ||
}); | ||
|
||
comp.addEventListener("onDummy", event -> { | ||
Thread.sleep(100); | ||
System.out.println(">> handle onDummy " + ++count); | ||
}); | ||
comp.appendChild(button); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
B101-ZK-5707.zul | ||
Purpose: | ||
Description: | ||
History: | ||
2024/9/19, Created by jumperchen | ||
Copyright (C) 2024 Potix Corporation. All Rights Reserved. | ||
--> | ||
<zk> | ||
<div apply="org.zkoss.zktest.test2.B101_ZK_5707Composer"/> | ||
</zk> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
zktest/src/test/java/org/zkoss/zktest/zats/test2/B101_ZK_5707Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* B101_ZK_5707Test.java | ||
Purpose: | ||
Description: | ||
History: | ||
12:48 PM 2024/9/19, Created by jumperchen | ||
Copyright (C) 2024 Potix Corporation. All Rights Reserved. | ||
*/ | ||
package org.zkoss.zktest.zats.test2; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
|
||
import org.zkoss.test.webdriver.ForkJVMTestOnly; | ||
import org.zkoss.zats.mimic.DesktopAgent; | ||
import org.zkoss.zk.ui.AbstractComponent; | ||
import org.zkoss.zktest.zats.ZATSTestCase; | ||
|
||
/** | ||
* @author jumperchen | ||
*/ | ||
@ForkJVMTestOnly | ||
public class B101_ZK_5707Test extends ZATSTestCase { | ||
private static Logger logger; | ||
@BeforeAll | ||
public static void beforeAll() throws Exception { | ||
logger = mock(Logger.class); | ||
setFinalStatic(AbstractComponent.class.getDeclaredField("log"), logger); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
DesktopAgent desktopAgent = connect("/test2/B101-ZK-5707.zul"); | ||
desktopAgent.query("button").click(); | ||
verify(logger, never()).warn(any()); | ||
} | ||
// https://stackoverflow.com/a/30703932 | ||
private static void setFinalStatic(Field field, Object newValue) throws Exception { | ||
field.setAccessible(true); | ||
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | ||
field.set(null, newValue); | ||
} | ||
} |