blob: b2b0b1f33ef1b4c960958f6f6e88de70c6c6e1fc [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Thomas Vachuskaecb63c52015-02-19 10:03:48 -08002 * Copyright 2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080016package org.onlab.util;
tom931af4e2014-09-13 12:00:57 -070017
Jonathan Hartecaa8a82015-02-04 09:44:08 -080018import org.junit.Ignore;
19import org.junit.Test;
20
21import java.util.List;
22import java.util.Timer;
23
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080024import static org.junit.Assert.*;
alshabib030111e2014-09-15 15:56:42 -070025import static org.onlab.junit.TestTools.delay;
tom931af4e2014-09-13 12:00:57 -070026
tom931af4e2014-09-13 12:00:57 -070027/**
28 * Tests the operation of the accumulator.
29 */
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080030public class AbstractAccumulatorTest {
tom931af4e2014-09-13 12:00:57 -070031
32 private final Timer timer = new Timer();
33
34 @Test
35 public void basics() throws Exception {
36 TestAccumulator accumulator = new TestAccumulator();
37 assertEquals("incorrect timer", timer, accumulator.timer());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080038 assertEquals("incorrect max events", 5, accumulator.maxItems());
tom931af4e2014-09-13 12:00:57 -070039 assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis());
40 assertEquals("incorrect idle ms", 50, accumulator.maxIdleMillis());
41 }
42
43 @Test
44 public void eventTrigger() {
45 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080046 accumulator.add(new TestItem("a"));
47 accumulator.add(new TestItem("b"));
48 accumulator.add(new TestItem("c"));
49 accumulator.add(new TestItem("d"));
tom931af4e2014-09-13 12:00:57 -070050 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080051 accumulator.add(new TestItem("e"));
Jonathan Hartecaa8a82015-02-04 09:44:08 -080052 delay(20);
tom931af4e2014-09-13 12:00:57 -070053 assertFalse("should have fired", accumulator.batch.isEmpty());
54 assertEquals("incorrect batch", "abcde", accumulator.batch);
55 }
56
Yuta HIGUCHI38935e52014-10-10 13:27:46 -070057 @Ignore("FIXME: timing sensitive test failing randomly.")
tom931af4e2014-09-13 12:00:57 -070058 @Test
59 public void timeTrigger() {
60 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080061 accumulator.add(new TestItem("a"));
tom093340b2014-10-10 00:15:36 -070062 delay(30);
tom931af4e2014-09-13 12:00:57 -070063 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080064 accumulator.add(new TestItem("b"));
tom093340b2014-10-10 00:15:36 -070065 delay(30);
tom931af4e2014-09-13 12:00:57 -070066 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080067 accumulator.add(new TestItem("c"));
tom093340b2014-10-10 00:15:36 -070068 delay(30);
69 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080070 accumulator.add(new TestItem("d"));
tom093340b2014-10-10 00:15:36 -070071 delay(30);
tom931af4e2014-09-13 12:00:57 -070072 assertFalse("should have fired", accumulator.batch.isEmpty());
tom093340b2014-10-10 00:15:36 -070073 assertEquals("incorrect batch", "abcd", accumulator.batch);
tom931af4e2014-09-13 12:00:57 -070074 }
75
76 @Test
77 public void idleTrigger() {
78 TestAccumulator accumulator = new TestAccumulator();
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080079 accumulator.add(new TestItem("a"));
tom931af4e2014-09-13 12:00:57 -070080 assertTrue("should not have fired yet", accumulator.batch.isEmpty());
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080081 accumulator.add(new TestItem("b"));
tom931af4e2014-09-13 12:00:57 -070082 delay(80);
83 assertFalse("should have fired", accumulator.batch.isEmpty());
84 assertEquals("incorrect batch", "ab", accumulator.batch);
85 }
86
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080087 private class TestItem {
88 private final String s;
89
90 public TestItem(String s) {
91 this.s = s;
92 }
93 }
94
95 private class TestAccumulator extends AbstractAccumulator<TestItem> {
tom931af4e2014-09-13 12:00:57 -070096
97 String batch = "";
98
99 protected TestAccumulator() {
100 super(timer, 5, 100, 50);
101 }
102
103 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800104 public void processItems(List<TestItem> items) {
105 for (TestItem item : items) {
106 batch += item.s;
tom931af4e2014-09-13 12:00:57 -0700107 }
108 }
109 }
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800110
tom931af4e2014-09-13 12:00:57 -0700111}