2022-06-09 08:16:51 +03:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj.event;
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2023-12-31 22:45:10 -08:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
2022-06-09 08:16:51 +03:00
|
|
|
|
2023-12-31 22:45:10 -08:00
|
|
|
import java.util.ConcurrentModificationException;
|
2022-06-09 08:16:51 +03:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
class EventLoopTest {
|
|
|
|
|
@Test
|
|
|
|
|
void testConditions() {
|
|
|
|
|
var counterTrue = new AtomicInteger(0);
|
|
|
|
|
var counterFalse = new AtomicInteger(0);
|
|
|
|
|
var loop = new EventLoop();
|
2022-11-28 23:48:48 +02:00
|
|
|
new BooleanEvent(loop, () -> true).ifHigh(counterTrue::incrementAndGet);
|
|
|
|
|
new BooleanEvent(loop, () -> false).ifHigh(counterFalse::incrementAndGet);
|
2022-06-09 08:16:51 +03:00
|
|
|
|
|
|
|
|
assertEquals(0, counterTrue.get());
|
|
|
|
|
assertEquals(0, counterFalse.get());
|
|
|
|
|
|
|
|
|
|
loop.poll();
|
|
|
|
|
|
|
|
|
|
assertEquals(1, counterTrue.get());
|
|
|
|
|
assertEquals(0, counterFalse.get());
|
|
|
|
|
|
|
|
|
|
loop.poll();
|
|
|
|
|
|
|
|
|
|
assertEquals(2, counterTrue.get());
|
|
|
|
|
assertEquals(0, counterFalse.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testClear() {
|
|
|
|
|
var condition = new AtomicBoolean(false);
|
|
|
|
|
var counter = new AtomicInteger(0);
|
|
|
|
|
var loop = new EventLoop();
|
|
|
|
|
|
|
|
|
|
// first ensure binding works
|
2022-11-28 23:48:48 +02:00
|
|
|
new BooleanEvent(loop, condition::get).ifHigh(counter::incrementAndGet);
|
2022-06-09 08:16:51 +03:00
|
|
|
|
|
|
|
|
condition.set(false);
|
|
|
|
|
loop.poll();
|
|
|
|
|
assertEquals(0, counter.get());
|
|
|
|
|
|
|
|
|
|
condition.set(true);
|
|
|
|
|
loop.poll();
|
|
|
|
|
assertEquals(1, counter.get());
|
|
|
|
|
|
|
|
|
|
// clear bindings
|
|
|
|
|
loop.clear();
|
|
|
|
|
|
|
|
|
|
condition.set(true);
|
|
|
|
|
loop.poll();
|
|
|
|
|
// shouldn't change
|
|
|
|
|
assertEquals(1, counter.get());
|
|
|
|
|
}
|
2023-12-31 22:45:10 -08:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testConcurrentModification() {
|
|
|
|
|
var loop = new EventLoop();
|
|
|
|
|
|
|
|
|
|
loop.bind(
|
|
|
|
|
() -> {
|
|
|
|
|
assertThrows(
|
|
|
|
|
ConcurrentModificationException.class,
|
|
|
|
|
() -> {
|
|
|
|
|
loop.bind(() -> {});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
loop.poll();
|
|
|
|
|
|
|
|
|
|
loop.clear();
|
|
|
|
|
|
|
|
|
|
loop.bind(
|
|
|
|
|
() -> {
|
|
|
|
|
assertThrows(
|
|
|
|
|
ConcurrentModificationException.class,
|
|
|
|
|
() -> {
|
|
|
|
|
loop.clear();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
loop.poll();
|
|
|
|
|
}
|
2022-06-09 08:16:51 +03:00
|
|
|
}
|