aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/yolean/concurrent/MemoizedTest.java
blob: f7eb77d5d3c256b9122b5d7ecb9319d2859856e0 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean.concurrent;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Phaser;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

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

    final Phaser phaser = new Phaser();
    final int threads = 128;

    @Test
    public void test() throws ExecutionException, InterruptedException {
        var lazy = new Memoized<>(new OnceSupplier(), OnceCloseable::close);
        phaser.register(); // test thread
        phaser.register(); // whoever calls the factory

        Phaser latch = new Phaser(threads + 1);
        ExecutorService executor = Executors.newFixedThreadPool(threads);
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < 128; i++) {
            futures.add(executor.submit(() -> {
                latch.arriveAndAwaitAdvance();
                lazy.get().rendezvous();
                while (true) lazy.get();
            }));
        }

        // All threads waiting for latch, will race to factory
        latch.arriveAndAwaitAdvance();

        // One thread waiting in factory, the others are blocked, will go to rendezvous
        phaser.arriveAndAwaitAdvance();

        // All threads waiting in rendezvous, will repeatedly get until failure
        phaser.arriveAndAwaitAdvance();

        // Unsynchronized close should be detected by all threads
        lazy.close();

        // Close should carry through only once
        lazy.close();

        assertEquals("already closed",
                     assertThrows(IllegalStateException.class, lazy::get).getMessage());

        for (Future<?> future : futures)
            assertEquals("java.lang.IllegalStateException: already closed",
                         assertThrows(ExecutionException.class, future::get).getMessage());

        executor.shutdown();
    }

    @Test
    public void closeBeforeFirstGet() throws Exception {
        OnceSupplier supplier = new OnceSupplier();
        Memoized<OnceCloseable, ?> lazy = Memoized.of(supplier);
        lazy.close();
        assertEquals("already closed",
                     assertThrows(IllegalStateException.class, lazy::get).getMessage());
        lazy.close();
        assertFalse(supplier.initialized.get());
    }

    class OnceSupplier implements Supplier<OnceCloseable> {
        final AtomicBoolean initialized = new AtomicBoolean();
        @Override public OnceCloseable get() {
            phaser.arriveAndAwaitAdvance();
            if ( ! initialized.compareAndSet(false, true)) fail("initialized more than once");
            phaser.bulkRegister(threads - 1); // register all the threads who didn't get the factory
            return new OnceCloseable();
        }
    }

    class OnceCloseable implements AutoCloseable {
        final AtomicBoolean closed = new AtomicBoolean();
        @Override public void close() {
            if ( ! closed.compareAndSet(false, true)) fail("closed more than once");
        }
        void rendezvous() {
            phaser.arriveAndAwaitAdvance();
        }
    }

}