|
| 1 | +/* |
| 2 | + * Copyright 2013 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.gwt.user.client; |
| 16 | + |
| 17 | +import com.google.gwt.core.client.Duration; |
| 18 | +import com.google.gwt.junit.client.GWTTestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Tests {@link Timer#cancel()} functionality. |
| 22 | + */ |
| 23 | +public class TimerCancelTest extends GWTTestCase { |
| 24 | + |
| 25 | + private static final class CountingTimer extends Timer { |
| 26 | + private int timerCount; |
| 27 | + @Override |
| 28 | + public void run() { |
| 29 | + timerCount++; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getModuleName() { |
| 35 | + return "com.google.gwt.user.UserTest"; |
| 36 | + } |
| 37 | + |
| 38 | + public void testCancelTimer() { |
| 39 | + final CountingTimer canceledTimer = new CountingTimer(); |
| 40 | + |
| 41 | + Timer cancelingTimer = new Timer() { |
| 42 | + @Override |
| 43 | + public void run() { |
| 44 | + assertEquals(0, canceledTimer.timerCount); |
| 45 | + canceledTimer.cancel(); |
| 46 | + } |
| 47 | + }; |
| 48 | + cancelingTimer.schedule(50); |
| 49 | + canceledTimer.schedule(100); |
| 50 | + |
| 51 | + |
| 52 | + busyWait(200); |
| 53 | + |
| 54 | + delayTestFinish(500); |
| 55 | + new Timer() { |
| 56 | + @Override |
| 57 | + public void run() { |
| 58 | + assertEquals(0, canceledTimer.timerCount); |
| 59 | + finishTest(); |
| 60 | + } |
| 61 | + }.schedule(300); |
| 62 | + } |
| 63 | + |
| 64 | + public void testRestartTimer() { |
| 65 | + final CountingTimer restartedTimer = new CountingTimer(); |
| 66 | + |
| 67 | + Timer cancelingTimer = new Timer() { |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + assertEquals(0, restartedTimer.timerCount); |
| 71 | + restartedTimer.cancel(); |
| 72 | + restartedTimer.schedule(100); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + cancelingTimer.schedule(50); |
| 77 | + restartedTimer.schedule(100); |
| 78 | + |
| 79 | + busyWait(200); |
| 80 | + |
| 81 | + delayTestFinish(500); |
| 82 | + new Timer() { |
| 83 | + @Override |
| 84 | + public void run() { |
| 85 | + assertEquals(1, restartedTimer.timerCount); |
| 86 | + finishTest(); |
| 87 | + } |
| 88 | + }.schedule(400); |
| 89 | + } |
| 90 | + |
| 91 | + private static void busyWait(double duration) { |
| 92 | + /* |
| 93 | + * It seems that IE adds an event to the javascript event loop immediately when a timer expires |
| 94 | + * (supposedly from a separate thread). After this has happened, canceling the timer has no |
| 95 | + * effect because it is already in the queue and no further checks are done when running the |
| 96 | + * event once it reaches the head of the queue. |
| 97 | + * |
| 98 | + * This means that to trigger the bug, we must ensure the timer has been added to the event loop |
| 99 | + * queue before it is canceled. To ensure this happens, we will busy wait until both timers |
| 100 | + * should have fired. This means the following happens: |
| 101 | + * |
| 102 | + * 1) While busy waiting, IE adds events for each timer to the event loop |
| 103 | + * |
| 104 | + * 2) IE pumps the event loop, running the helper timer that cancels the tested timer. This does |
| 105 | + * however not have any effect because the timer is already in the event loop queue. |
| 106 | + * |
| 107 | + * 3) IE pumps the event loop again and runs the event for the tested timer, without realizing |
| 108 | + * that it has been canceled. |
| 109 | + * |
| 110 | + * Without busy waiting, the tested timer would not yet have been added to the event loop queue |
| 111 | + * at the point when the timer is canceled, in which case canceling the timer would work as |
| 112 | + * expected. |
| 113 | + * |
| 114 | + * If the two timers are not scheduled in the same order that they will run, it seems that IE |
| 115 | + * does some additional checks that makes the problem disappear. |
| 116 | + */ |
| 117 | + |
| 118 | + double start = Duration.currentTimeMillis(); |
| 119 | + while (Duration.currentTimeMillis() - start <= duration) { |
| 120 | + // Busy wait |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments