use of com.infiniteautomation.mango.spring.components.RunAs in project ma-core-public by infiniteautomation.
the class UserEventsTest method testListenerAddRemoveSyncrhonization.
@Test
public void testListenerAddRemoveSyncrhonization() throws InterruptedException {
List<User> users = createUsers(1, PermissionHolder.SUPERADMIN_ROLE);
ExecutorService executor = Executors.newFixedThreadPool(3);
SynchronousQueue<MockUserEventListener> queue = new SynchronousQueue<>();
MockUserEventListener l = new MockUserEventListener(users.get(0));
AtomicBoolean removerRunning = new AtomicBoolean(true);
AtomicBoolean generatorRunning = new AtomicBoolean(true);
RunAs runAs = Common.getBean(RunAs.class);
Runnable adder = () -> {
while (removerRunning.get()) {
try {
MockUserEventListener adding = queue.take();
Common.eventManager.addUserEventListener(adding);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Runnable remover = () -> {
while (generatorRunning.get()) {
try {
queue.put(l);
Common.eventManager.removeUserEventListener(l);
} catch (Exception e) {
e.printStackTrace();
}
}
removerRunning.set(false);
};
// Event Generator Thread
Runnable generator = () -> {
runAs.runAs(PermissionHolder.SYSTEM_SUPERADMIN, () -> {
// Raise some events
List<MockEventTypeTime> raised = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
MockEventType event = new MockEventType(DuplicateHandling.ALLOW, null, i, dataPointId, null);
raised.add(new MockEventTypeTime(event, timer.currentTimeMillis()));
Common.eventManager.raiseEvent(event, timer.currentTimeMillis(), true, AlarmLevels.URGENT, new TranslatableMessage("common.default", "Mock Event"), null);
timer.fastForwardTo(timer.currentTimeMillis() + 1);
}
generatorRunning.set(false);
});
};
executor.execute(adder);
executor.execute(remover);
executor.execute(generator);
int maxWaitSeconds = 30;
for (int i = maxWaitSeconds; i > 0; i--) {
if (!generatorRunning.get()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
executor.shutdown();
executor.awaitTermination(10000, TimeUnit.MILLISECONDS);
executor.shutdownNow();
}
use of com.infiniteautomation.mango.spring.components.RunAs in project ma-core-public by infiniteautomation.
the class EventInstanceWithTagsQueryTest method before.
@Before
public void before() {
super.before();
service = Common.getBean(EventInstanceService.class);
eventDetectorsService = Common.getBean(EventDetectorsService.class);
dao = Common.getBean(EventDao.class);
roleService = Common.getBean(RoleService.class);
runAs = Common.getBean(RunAs.class);
setupRoles();
AtomicInteger count = new AtomicInteger();
List<String> tagKeys = Stream.generate(() -> "key" + count.getAndIncrement()).limit(2).collect(Collectors.toList());
allTags = tagKeys.stream().collect(Collectors.toMap(Function.identity(), k -> k + "_value" + random.nextInt(10)));
MockDataSourceVO ds = createMockDataSource(true);
// Insert 2 data points
point1 = createMockDataPoint(ds, (dp) -> {
key1 = tagKeys.get(0);
value1 = allTags.get(tagKeys.get(0));
Map<String, String> tags = new HashMap<>();
tags.put(key1, value1);
dp.setTags(tags);
dp.setReadPermission(MangoPermission.requireAllRoles(point1ReadRole));
});
point2 = createMockDataPoint(ds, (dp) -> {
key2 = tagKeys.get(1);
value2 = allTags.get(tagKeys.get(1));
Map<String, String> tags = new HashMap<>();
tags.put(key2, value2);
dp.setTags(tags);
dp.setReadPermission(MangoPermission.requireAllRoles(point2ReadRole));
});
// Create the event detectors
runAs.runAs(runAs.systemSuperadmin(), () -> {
AnalogChangeEventDetectorDefinition definition = (AnalogChangeEventDetectorDefinition) ModuleRegistry.getEventDetectorDefinition(AnalogChangeEventDetectorDefinition.TYPE_NAME);
AnalogChangeDetectorVO detector = definition.baseCreateEventDetectorVO(point1);
detector.setName("Detector for point " + point1.getName());
eventDetectorsService.insert(detector);
detector1 = detector;
});
runAs.runAs(runAs.systemSuperadmin(), () -> {
AnalogChangeEventDetectorDefinition definition = (AnalogChangeEventDetectorDefinition) ModuleRegistry.getEventDetectorDefinition(AnalogChangeEventDetectorDefinition.TYPE_NAME);
AnalogChangeDetectorVO detector = definition.baseCreateEventDetectorVO(point2);
detector.setName("Detector for point " + point2.getName());
eventDetectorsService.insert(detector);
detector2 = detector;
});
}
Aggregations