/**
 * Copyright 2020 Viridian Software Ltd.
 */
package org.mini2Dx.miniscript.core;

import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.PriorityBlockingQueue;

public class ScriptInvocationTest {
	private final PriorityBlockingQueue<ScriptInvocation> queue = new PriorityBlockingQueue<>();

	@Test
	public void testPriority1() {
		final ScriptInvocation invocation1 = new ScriptInvocation(null);
		final ScriptInvocation invocation2 = new ScriptInvocation(null);

		invocation1.setPriority(0);
		invocation2.setPriority(1000);

		queue.offer(invocation1);
		queue.offer(invocation2);

		Assert.assertEquals(invocation2, queue.poll());
		Assert.assertEquals(invocation1, queue.poll());

		queue.offer(invocation2);
		queue.offer(invocation1);

		Assert.assertEquals(invocation2, queue.poll());
		Assert.assertEquals(invocation1, queue.poll());
	}

	@Test
	public void testPriority2() {
		final ScriptInvocation invocation1 = new ScriptInvocation(null);
		final ScriptInvocation invocation2 = new ScriptInvocation(null);

		invocation1.setPriority(9999);
		invocation2.setPriority(100);

		queue.offer(invocation1);
		queue.offer(invocation2);

		Assert.assertEquals(invocation1, queue.poll());
		Assert.assertEquals(invocation2, queue.poll());

		queue.offer(invocation2);
		queue.offer(invocation1);

		Assert.assertEquals(invocation1, queue.poll());
		Assert.assertEquals(invocation2, queue.poll());
	}

	@Test
	public void testSamePriority() {
		final ScriptInvocation invocation1 = new ScriptInvocation(null);
		final ScriptInvocation invocation2 = new ScriptInvocation(null);

		invocation1.setPriority(0);
		invocation2.setPriority(0);

		queue.offer(invocation1);
		queue.offer(invocation2);

		Assert.assertEquals(invocation1, queue.poll());
		Assert.assertEquals(invocation2, queue.poll());

		queue.offer(invocation2);
		queue.offer(invocation1);

		Assert.assertEquals(invocation2, queue.poll());
		Assert.assertEquals(invocation1, queue.poll());
	}
}

@@ -129,16 +129,36 @@
 .Test;%0A%0A
+import java.util.*;%0A
 import j
@@ -2000,11 +2000,707 @@
 l());%0A%09%7D
+%0A%0A%09@Test%0A%09public void testPriority() %7B%0A%09%09final Random random = new Random();%0A%09%09final List%3CInteger%3E unordered = new ArrayList%3C%3E();%0A%0A%09%09for(int i = 0; i %3C 1000; i++) %7B%0A%09%09%09final int priority = random.nextInt();%0A%09%09%09final ScriptInvocation invocation = new ScriptInvocation(null);%0A%09%09%09invocation.setPriority(priority);%0A%09%09%09unordered.add(priority);%0A%0A%09%09%09queue.offer(invocation);%0A%09%09%7D%0A%0A%09%09final List%3CInteger%3E ordered = new ArrayList%3C%3E(unordered);%0A%09%09Collections.sort(ordered);%0A%09%09Collections.reverse(ordered);%0A%0A%09%09for(int i = 0; i %3C 1000; i++) %7B%0A%09%09%09final int expectedPriority = ordered.get(i);%0A%09%09%09final ScriptInvocation result = queue.poll();%0A%09%09%09Assert.assertEquals(expectedPriority, result.getPriority());%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add additional tests to ScriptInvocationTest