Project

General

Profile

1
package eu.dnetlib.enabling.tools.blackboard;
2

    
3
import org.junit.Test;
4
import org.junit.runner.RunWith;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
7
import org.springframework.test.context.ContextConfiguration;
8
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9

    
10
@RunWith(value = SpringJUnit4ClassRunner.class)
11
@ContextConfiguration
12
public class NotificationHandlerChainTest {
13

    
14
	@Autowired
15
	private transient ThreadPoolTaskExecutor executor;
16

    
17
	@Test
18
	public void testDelegateNotification() throws InterruptedException {
19
		System.out.println(executor);
20

    
21
		System.out.println("executing");
22

    
23
		for (int i = 0; i < 100; i++)
24
			executor.execute(new Job(i));
25

    
26
		System.out.println("executed - waiting");
27
		Thread.sleep(2000);
28

    
29
		System.out.println("active count: " + executor.getActiveCount());
30
		System.out.println("current pool size: " + executor.getCorePoolSize());
31
		System.out.println("pool size " + executor.getPoolSize());
32

    
33
		Thread.sleep(3000);
34

    
35
		System.out.println("ok");
36
	}
37

    
38
	@Test
39
	public void testRecursive() throws InterruptedException {
40

    
41
		for (int i = 0; i < 4; i++)
42
			executor.execute(new RecJob(i * 10, 4));
43

    
44
		System.out.println("executed - waiting");
45
		Thread.sleep(2000);
46

    
47
		System.out.println("active count: " + executor.getActiveCount());
48
		System.out.println("current pool size: " + executor.getCorePoolSize());
49
		System.out.println("pool size " + executor.getPoolSize());
50

    
51
		Thread.sleep(3000);
52

    
53
		System.out.println("ok");
54
	}
55

    
56
	private static final class Job implements Runnable {
57

    
58
		private final int value;
59

    
60
		public Job(final int i) {
61
			value = i;
62
		}
63

    
64
		@Override
65
		public void run() {
66
			System.out.println("thread started - " + value);
67
			try {
68
				Thread.sleep(4000);
69
			} catch (final InterruptedException e) {
70
				//
71
			}
72
			System.out.println("thread finished - " + value);
73
		}
74
	}
75

    
76
	public class RecJob implements Runnable {
77

    
78
		private int times;
79
		private int num;
80

    
81
		public RecJob(int num, int times) {
82
			this.num = num;
83
			this.times = times;
84
		}
85

    
86
		@Override
87
		public void run() {
88
			System.out.println("starting " + num);
89
			if (times >= 0)
90
				executor.execute(new RecJob(num + 1, times - 1));
91
			try {
92
				Thread.sleep(4000);
93
			} catch (InterruptedException e) {
94
				// TODO Auto-generated catch block
95
				e.printStackTrace();
96
			}
97

    
98
			System.out.println("thread finished - " + num);
99
		}
100

    
101
	}
102

    
103
}
    (1-1/1)