File: BackoffTest.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (106 lines) | stat: -rw-r--r-- 3,484 bytes parent folder | download | duplicates (7)
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
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * BackoffTest.cpp
 * Test fixture for the TCPConnector class
 * Copyright (C) 2012 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>
#include <stdint.h>
#include <string.h>
#include <iostream>
#include <string>

#include "ola/Callback.h"
#include "ola/Clock.h"
#include "ola/util/Backoff.h"
#include "ola/testing/TestUtils.h"


using ola::BackoffGenerator;
using ola::ExponentialBackoffPolicy;
using ola::LinearBackoffPolicy;
using ola::TimeInterval;

class BackoffTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(BackoffTest);

  CPPUNIT_TEST(testLinearBackoffPolicy);
  CPPUNIT_TEST(testExponentialBackoffPolicy);
  CPPUNIT_TEST(testBackoffGenerator);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testLinearBackoffPolicy();
    void testExponentialBackoffPolicy();
    void testBackoffGenerator();
};

CPPUNIT_TEST_SUITE_REGISTRATION(BackoffTest);


/**
 * Test the linear backoff policy.
 */
void BackoffTest::testLinearBackoffPolicy() {
  // 5 per attempt, up to a max of 30
  LinearBackoffPolicy policy(TimeInterval(5, 0), TimeInterval(30, 0));

  OLA_ASSERT_EQ(TimeInterval(5, 0), policy.BackOffTime(1));
  OLA_ASSERT_EQ(TimeInterval(10, 0), policy.BackOffTime(2));
  OLA_ASSERT_EQ(TimeInterval(15, 0), policy.BackOffTime(3));

  OLA_ASSERT_EQ(TimeInterval(30, 0), policy.BackOffTime(6));
  OLA_ASSERT_EQ(TimeInterval(30, 0), policy.BackOffTime(7));
}


/**
 * Test the exponential backoff policy.
 */
void BackoffTest::testExponentialBackoffPolicy() {
  // start with 10s, up to 170s.
  ExponentialBackoffPolicy policy(TimeInterval(10, 0), TimeInterval(170, 0));

  OLA_ASSERT_EQ(TimeInterval(10, 0), policy.BackOffTime(1));
  OLA_ASSERT_EQ(TimeInterval(20, 0), policy.BackOffTime(2));
  OLA_ASSERT_EQ(TimeInterval(40, 0), policy.BackOffTime(3));
  OLA_ASSERT_EQ(TimeInterval(80, 0), policy.BackOffTime(4));
  OLA_ASSERT_EQ(TimeInterval(160, 0), policy.BackOffTime(5));
  OLA_ASSERT_EQ(TimeInterval(170, 0), policy.BackOffTime(6));
  OLA_ASSERT_EQ(TimeInterval(170, 0), policy.BackOffTime(7));
}


/**
 * Test the BackoffGenerator
 */
void BackoffTest::testBackoffGenerator() {
  BackoffGenerator generator(
      new LinearBackoffPolicy(TimeInterval(5, 0), TimeInterval(30, 0)));

  OLA_ASSERT_EQ(TimeInterval(5, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(10, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(15, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(20, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(25, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(30, 0), generator.Next());

  generator.Reset();
  OLA_ASSERT_EQ(TimeInterval(5, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(10, 0), generator.Next());
  OLA_ASSERT_EQ(TimeInterval(15, 0), generator.Next());
}