NodeRefHourAggTestCase.java 3.5 KB
Newer Older
P
pengys5 已提交
1 2 3 4 5 6 7
package com.a.eye.skywalking.collector.worker.noderef.persistence;

import com.a.eye.skywalking.collector.actor.ClusterWorkerContext;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.ProviderNotFoundException;
import com.a.eye.skywalking.collector.actor.WorkerRefs;
import com.a.eye.skywalking.collector.actor.selector.HashCodeSelector;
P
pengys5 已提交
8
import com.a.eye.skywalking.collector.worker.config.WorkerConfig;
P
pengys5 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22
import com.a.eye.skywalking.collector.worker.mock.RecordDataAnswer;
import com.a.eye.skywalking.collector.worker.storage.RecordData;
import com.a.eye.skywalking.collector.worker.tools.RecordDataAggTools;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

23 24
import java.util.TimeZone;

P
pengys5 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import static org.mockito.Mockito.*;

/**
 * @author pengys5
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({LocalWorkerContext.class})
@PowerMockIgnore({"javax.management.*"})
public class NodeRefHourAggTestCase {

    private NodeRefHourAgg agg;
    private RecordDataAnswer recordDataAnswer;
    private ClusterWorkerContext clusterWorkerContext;
    private LocalWorkerContext localWorkerContext;

    @Before
    public void init() throws Exception {
42 43 44
        System.setProperty("user.timezone", "UTC");
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

P
pengys5 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        clusterWorkerContext = PowerMockito.mock(ClusterWorkerContext.class);

        localWorkerContext = PowerMockito.mock(LocalWorkerContext.class);
        WorkerRefs workerRefs = mock(WorkerRefs.class);

        recordDataAnswer = new RecordDataAnswer();
        doAnswer(recordDataAnswer).when(workerRefs).tell(Mockito.any(RecordData.class));

        when(localWorkerContext.lookup(NodeRefHourSave.Role.INSTANCE)).thenReturn(workerRefs);
        agg = new NodeRefHourAgg(NodeRefHourAgg.Role.INSTANCE, clusterWorkerContext, localWorkerContext);
    }

    @Test
    public void testRole() {
        Assert.assertEquals(NodeRefHourAgg.class.getSimpleName(), NodeRefHourAgg.Role.INSTANCE.roleName());
        Assert.assertEquals(HashCodeSelector.class.getSimpleName(), NodeRefHourAgg.Role.INSTANCE.workerSelector().getClass().getSimpleName());
    }

    @Test
    public void testFactory() {
P
Fix 159  
pengys5 已提交
65 66 67
        NodeRefHourAgg.Factory factory = new NodeRefHourAgg.Factory();
        Assert.assertEquals(NodeRefHourAgg.class.getSimpleName(), factory.role().roleName());
        Assert.assertEquals(NodeRefHourAgg.class.getSimpleName(), factory.workerInstance(null).getClass().getSimpleName());
P
pengys5 已提交
68 69

        int testSize = 10;
70
        WorkerConfig.WorkerNum.NodeRef.NodeRefHourAgg.VALUE = testSize;
P
Fix 159  
pengys5 已提交
71
        Assert.assertEquals(testSize, factory.workerNum());
P
pengys5 已提交
72 73 74 75
    }

    @Test
    public void testPreStart() throws ProviderNotFoundException {
P
Fix 159  
pengys5 已提交
76
        when(clusterWorkerContext.findProvider(NodeRefHourSave.Role.INSTANCE)).thenReturn(new NodeRefHourSave.Factory());
P
pengys5 已提交
77 78 79 80 81 82 83 84 85 86 87

        ArgumentCaptor<NodeRefHourSave.Role> argumentCaptor = ArgumentCaptor.forClass(NodeRefHourSave.Role.class);
        agg.preStart();
        verify(clusterWorkerContext).findProvider(argumentCaptor.capture());
    }

    @Test
    public void testOnWork() throws Exception {
        RecordDataAggTools.INSTANCE.testOnWork(agg, recordDataAnswer);
    }
}