aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java
blob: f54c2f0bb3f119c25f361612bee2405e7fe9428f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
 * @author jonmv
 */
public class StripedExecutorTest {

    private static final int workers = 1 << 5;
    private static final int values = 1 << 10;

    @Test
    public void testSerialization() {
        AtomicLong counter = new AtomicLong(0);
        List<Deque<Long>> sequences = new ArrayList<>();
        for (int j = 0; j < workers; j++)
            sequences.add(new ConcurrentLinkedDeque<>());

        StripedExecutor<Integer> executor = new StripedExecutor<>();
        for (int i = 0; i < values; i++)
            for (int j = 0; j < workers; j++) {
                Deque<Long> sequence = sequences.get(j);
                executor.execute(j, () -> sequence.add(counter.incrementAndGet()));
            }
        executor.shutdownAndWait();

        for (int j = 0; j < workers; j++) {
            assertEquals(values, sequences.get(j).size());
            assertArrayEquals(sequences.get(j).stream().sorted().toArray(Long[]::new),
                              sequences.get(j).toArray(Long[]::new));
        }
    }

}