blob: 837ffd58e76b801ea110c5bce10b1d0017361f52 [file] [log] [blame]
Éric Lemoine71272712016-11-08 12:53:51 +00001-- Copyright 2016 Mirantis, Inc.
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- You may obtain a copy of the License at
6--
7-- http://www.apache.org/licenses/LICENSE-2.0
8--
9-- Unless required by applicable law or agreed to in writing, software
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14
15EXPORT_ASSERT_TO_GLOBALS=true
16require('luaunit')
17require('os')
18package.path = package.path .. ";../heka/files/lua/common/?.lua;lua/mocks/?.lua"
19
20local accumulator = require('accumulator')
21
22TestAccumulator = {}
23
24 function TestAccumulator:test_flush_on_append()
25 local sentinel = false
26 local function test_cb(items)
27 assertEquals(#items, 3)
28 sentinel = true
29 end
30 local accum = accumulator.new(2, 5, test_cb)
31 accum:append(1)
32 assertEquals(sentinel, false)
33 accum:append(2)
34 assertEquals(sentinel, false)
35 accum:append(3)
36 assertEquals(sentinel, true)
37 end
38
39 function TestAccumulator:test_flush_interval_with_buffer()
40 local now = os.time()
41 local sentinel = false
42 local function test_cb(items)
43 assertEquals(#items, 1)
44 sentinel = true
45 end
46 local accum = accumulator.new(20, 1, test_cb)
47 accum:append(1)
48 assertEquals(sentinel, false)
49 accum:flush((now + 2) * 1e9)
50 assertEquals(sentinel, true)
51 end
52
53 function TestAccumulator:test_flush_interval_with_empty_buffer()
54 local now = os.time()
55 local sentinel = false
56 local function test_cb(items)
57 assertEquals(#items, 0)
58 sentinel = true
59 end
60 local accum = accumulator.new(20, 1, test_cb)
61 accum:flush((now + 2) * 1e9)
62 assertEquals(sentinel, true)
63 end
64
65lu = LuaUnit
66lu:setVerbosity( 1 )
67os.exit( lu:run() )
68