1 /*
2 * SymmetricDS is an open source database synchronization solution.
3 *
4 * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20 package org.jumpmind.symmetric.util;
21
22 import java.io.IOException;
23 import java.util.Random;
24
25 import org.junit.Test;
26
27 public class MeteredOutputStreamTest {
28
29 @Test
30 public static void testBasic() throws IOException {
31 final long rate = 5 * 1024;
32 final long count = 20;
33 final int bufferSize = 8192;
34
35 MeteredOutputStream out = new MeteredOutputStream(new NullOutputStream(), rate, 8192, 1);
36 long start = System.currentTimeMillis();
37 byte[] testBytes = new byte[bufferSize];
38
39 Random r = new Random();
40
41 r.nextBytes(testBytes);
42
43 long i = 0;
44 for (i = 0; i < count; i++) {
45 out.write(testBytes, 0, testBytes.length);
46
47 if ((i % 10) == 0) {
48 System.out.print('#');
49 }
50 }
51
52 double expectedTime = (bufferSize * count) / rate;
53 double actualTime = (System.currentTimeMillis() - start + 1) / 1000;
54 assert (actualTime >= expectedTime - 2 && actualTime <= expectedTime + 2);
55 }
56 }