aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/application/MetricImplTestCase.java
blob: 33897d8eacc97bfce0fc937b1781daad667883a3 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.yahoo.jdisc.Metric;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;


/**
 * @author Simon Thoresen Hult
 */
public class MetricImplTestCase {

    @Test
    void requireThatClassIsInjectedByDefault() {
        Metric metric = Guice.createInjector().getInstance(Metric.class);
        assertTrue(metric instanceof MetricImpl);
    }

    @Test
    void requireThatConsumerIsOptional() {
        Injector injector = Guice.createInjector();
        Metric metric = injector.getInstance(Metric.class);
        metric.set("foo", 6, null);
        metric.add("foo", 9, null);
    }

    @Test
    void requireThatConsumerIsCalled() throws InterruptedException {
        final MyConsumer consumer = new MyConsumer();
        Injector injector = Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(MetricConsumer.class).toInstance(consumer);
            }
        });
        Metric metric = injector.getInstance(Metric.class);
        metric.set("foo", 6, null);
        assertEquals(6, consumer.map.get("foo").intValue());
        metric.add("foo", 9, null);
        assertEquals(15, consumer.map.get("foo").intValue());
        Metric.Context ctx = metric.createContext(null);
        assertEquals(consumer.ctx, ctx);
    }

    @Test
    void requireThatWorkerMetricHasPrecedence() throws InterruptedException {
        final MyConsumer globalConsumer = new MyConsumer();
        Injector injector = Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(MetricConsumer.class).toInstance(globalConsumer);
            }
        });
        Metric metric = injector.getInstance(Metric.class);

        MyConsumer localConsumer = new MyConsumer();
        localConsumer.latchRef.set(new CountDownLatch(1));
        new ContainerThread(new SetTask(metric, "foo", 6), localConsumer).start();
        localConsumer.latchRef.get().await(600, TimeUnit.SECONDS);
        assertEquals(6, localConsumer.map.get("foo").intValue());
        assertTrue(globalConsumer.map.isEmpty());

        localConsumer.latchRef.set(new CountDownLatch(1));
        new ContainerThread(new AddTask(metric, "foo", 9), localConsumer).start();
        localConsumer.latchRef.get().await(600, TimeUnit.SECONDS);
        assertEquals(15, localConsumer.map.get("foo").intValue());
        assertTrue(globalConsumer.map.isEmpty());
    }

    private static class SetTask implements Runnable {

        final Metric metric;
        final String key;
        final Number val;

        public SetTask(Metric metric, String key, Number val) {
            this.metric = metric;
            this.key = key;
            this.val = val;
        }

        @Override
        public void run() {
            metric.set(key, val, null);
        }
    }

    private static class AddTask implements Runnable {

        final Metric metric;
        final String key;
        final Number val;

        public AddTask(Metric metric, String key, Number val) {
            this.metric = metric;
            this.key = key;
            this.val = val;
        }

        @Override
        public void run() {
            metric.add(key, val, null);
        }
    }

    private static class MyConsumer implements MetricConsumer {

        final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>();
        final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>();
        final Metric.Context ctx = new Metric.Context() { };

        @Override
        public void set(String key, Number val, Metric.Context ctx) {
            map.put(key, val.intValue());
            CountDownLatch latch = latchRef.get();
            if (latch != null) {
                latch.countDown();
            }
        }

        @Override
        public void add(String key, Number val, Metric.Context ctx) {
            map.put(key, map.get(key) + val.intValue());
            CountDownLatch latch = this.latchRef.get();
            if (latch != null) {
                latch.countDown();
            }
        }

        @Override
        public Metric.Context createContext(Map<String, ?> properties) {
            return ctx;
        }
    }
}