summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/operationProcessor/ConcurrentDocumentOperationBlockerTest.java
blob: 99df48e2c70f91f9c683095dab54d87ff71684b1 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.operationProcessor;

import com.yahoo.vespa.http.client.core.operationProcessor.ConcurrentDocumentOperationBlocker;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class ConcurrentDocumentOperationBlockerTest {

    final ConcurrentDocumentOperationBlocker blocker = new ConcurrentDocumentOperationBlocker();
    final CountDownLatch latch = new CountDownLatch(1);

    @Before
    public void setup() throws InterruptedException {
        blocker.setMaxConcurrency(2);
        blocker.startOperation();
        assertThat(blocker.availablePermits(), is(1));
        blocker.startOperation();
    }

    private void spawnThreadPushOperationThenCountDown() {
        new Thread(() -> {
            try {
                blocker.startOperation();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
        }).start();
    }

    @Test
    public void testBasics() throws InterruptedException {
        spawnThreadPushOperationThenCountDown();
        assertFalse(latch.await(10, TimeUnit.MILLISECONDS));
        blocker.operationDone();
        assertTrue(latch.await(120, TimeUnit.SECONDS));
    }

    @Test
    public void testResizeLarger() throws InterruptedException {
        spawnThreadPushOperationThenCountDown();
        assertFalse(latch.await(10, TimeUnit.MILLISECONDS));
        blocker.setMaxConcurrency(3);
        assertTrue(latch.await(120, TimeUnit.SECONDS));
    }

    @Test
    public void testResizeSmaller() throws InterruptedException {
        spawnThreadPushOperationThenCountDown();
        blocker.setMaxConcurrency(1);
        blocker.operationDone();
        assertFalse(latch.await(10, TimeUnit.MILLISECONDS));
        blocker.operationDone();
        assertTrue(latch.await(120, TimeUnit.SECONDS));
    }
}