001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.util; 017 018import static org.junit.Assert.assertEquals; 019import org.junit.Before; 020import org.junit.Test; 021 022/** 023 * @author Randall Hauch (rhauch@redhat.com) 024 */ 025public class TimeBasedKeysTest { 026 027 protected TimeBasedKeys counter = TimeBasedKeys.create(); 028 private boolean print; 029 030 @Before 031 public void beforeEach() { 032 this.print = false; 033 } 034 035 @Test 036 public void shouldCorrectlyCalculateFirstAndLastCounterFor1BitCounter() { 037 TimeBasedKeys counter = TimeBasedKeys.create(1); 038 assertEquals(0L, counter.getCounterStartingAt(0L)); 039 assertEquals(1L, counter.getCounterEndingAt(0L)); 040 } 041 042 @Test 043 public void shouldCorrectlyCalculateFirstAndLastCounterFor2Bit() { 044 TimeBasedKeys counter = TimeBasedKeys.create(2); 045 assertEquals(0L, counter.getCounterStartingAt(0L)); 046 assertEquals(3L, counter.getCounterEndingAt(0L)); 047 } 048 049 @Test 050 public void shouldCorrectlyCalculateFirstAndLastCounterFor3Bit() { 051 TimeBasedKeys counter = TimeBasedKeys.create(3); 052 assertEquals(0L, counter.getCounterStartingAt(0L)); 053 assertEquals(7L, counter.getCounterEndingAt(0L)); 054 } 055 056 @Test 057 public void shouldCorrectlyCalculateFirstAndLastCounterFor4Bit() { 058 TimeBasedKeys counter = TimeBasedKeys.create(4); 059 assertEquals(0L, counter.getCounterStartingAt(0L)); 060 assertEquals(15L, counter.getCounterEndingAt(0L)); 061 } 062 063 @Test 064 public void shouldCorrectlyCalculateFirstAndLastCounterFor5Bit() { 065 TimeBasedKeys counter = TimeBasedKeys.create(5); 066 assertEquals(0L, counter.getCounterStartingAt(0L)); 067 assertEquals(31L, counter.getCounterEndingAt(0L)); 068 } 069 070 @Test 071 public void shouldCorrectlyCalculateFirstAndLastCounterFor6Bit() { 072 TimeBasedKeys counter = TimeBasedKeys.create(6); 073 assertEquals(0L, counter.getCounterStartingAt(0L)); 074 assertEquals(63L, counter.getCounterEndingAt(0L)); 075 } 076 077 @Test 078 public void shouldCorrectlyCalculateFirstAndLastCounterFor7Bit() { 079 TimeBasedKeys counter = TimeBasedKeys.create(7); 080 assertEquals(0L, counter.getCounterStartingAt(0L)); 081 assertEquals(127L, counter.getCounterEndingAt(0L)); 082 } 083 084 @Test 085 public void shouldCorrectlyCalculateFirstAndLastCounterFor8Bit() { 086 TimeBasedKeys counter = TimeBasedKeys.create(8); 087 assertEquals(0L, counter.getCounterStartingAt(0L)); 088 assertEquals(255L, counter.getCounterEndingAt(0L)); 089 } 090 091 @Test 092 public void shouldCorrectlyCalculateFirstAndLastCounterFor16Bit() { 093 long maxValue = ((long)Math.pow(2, 16)) - 1; 094 TimeBasedKeys counter = TimeBasedKeys.create(16); 095 assertEquals(0L, counter.getCounterStartingAt(0L)); 096 assertEquals(maxValue, counter.getCounterEndingAt(0L)); 097 } 098 099 @Test 100 public void shouldObtain10MillionCountersThreadSafe() { 101 print(counter.nextKey()); 102 for (int i = 0; i != 10000000; ++i) { 103 counter.nextKey(); 104 } 105 print(counter.nextKey()); 106 } 107 108 @Test 109 public void shouldObtain10MillionCountersFromThreadSafeUsingMultipleThreads() { 110 print(counter.nextKey()); 111 for (int j = 0; j != 100; ++j) { 112 new Thread(new Runnable() { 113 @Override 114 public void run() { 115 for (int i = 0; i != 100000; ++i) { 116 counter.nextKey(); 117 } 118 } 119 }).run(); 120 } 121 print(counter.nextKey()); 122 } 123 124 protected void print( String str ) { 125 if (print) System.out.println(str); 126 } 127 128 protected void print( long value ) { 129 if (print) System.out.println(value); 130 } 131}