@Test public void testGetReadBucketEmpty() throws Exception { work = new File(System.getProperty("java.io.tmpdir", "."), "Puma"); try { FileUtils.deleteDirectory(work); } catch (IOException e1) { e1.printStackTrace(); } this.localBucketIndex.start(); try { Bucket bucket = this.localBucketIndex.getReadBucket(-1, true); Assert.assertEquals(null, bucket); bucket = this.localBucketIndex.getReadBucket(-2, true); Assert.assertEquals(null, bucket); Sequence seq = new Sequence(120710, 0); bucket = this.localBucketIndex.getReadBucket(seq.longValue(), true); Assert.assertEquals(null, bucket); } catch (StorageClosedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
@Test public void testGetReadBucket() throws Exception { System.out.println("*************************************************************"); System.out.println("*********************testGetReadBucket***********************"); System.out.println("*************************************************************"); try { for (int i = 0; i < 4; i++) { work = new File( System.getProperty("java.io.tmpdir", "."), "Puma/20120711/bucket-" + Integer.toString(i)); work.getParentFile().mkdirs(); if (work.createNewFile()) { System.out.println("create a file: " + work.getName()); } } } catch (IOException e1) { e1.printStackTrace(); } this.localBucketIndex.start(); try { Bucket bucket = this.localBucketIndex.getReadBucket(-1, true); Assert.assertEquals(120710, bucket.getStartingSequece().getCreationDate()); Assert.assertEquals(0, bucket.getStartingSequece().getNumber()); bucket.stop(); bucket = this.localBucketIndex.getReadBucket(-2, true); Assert.assertEquals(null, bucket); Sequence seq = new Sequence(120711, 3); this.localBucketIndex.updateLatestSequence(seq); bucket = this.localBucketIndex.getReadBucket(-2, true); Assert.assertEquals(120711, bucket.getStartingSequece().getCreationDate()); Assert.assertEquals(3, bucket.getStartingSequece().getNumber()); bucket.stop(); DdlEvent event = new DdlEvent(); event.setSql("CREATE TABLE products (proeduct VARCHAR(10))"); event.setDatabase("cat"); event.setExecuteTime(0); event.setSeq(seq.longValue()); event.setTable(null); JsonEventCodec codec = new JsonEventCodec(); byte[] data = null; try { data = codec.encode(event); } catch (IOException e) { e.printStackTrace(); } try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(ByteArrayUtils.intToByteArray(data.length)); bos.write(data); RandomAccessFile file = new RandomAccessFile( new File(System.getProperty("java.io.tmpdir", "."), "Puma/20120711/bucket-3"), "rw"); file.write(bos.toByteArray()); bos.close(); file.close(); bucket = this.localBucketIndex.getReadBucket(seq.longValue(), true); Assert.assertEquals(120711, bucket.getStartingSequece().getCreationDate()); Assert.assertEquals(3, bucket.getStartingSequece().getNumber()); bucket.stop(); } catch (IOException e) { e.printStackTrace(); } bucket = this.localBucketIndex.getReadBucket(seq.longValue(), true); Assert.assertEquals(120711, bucket.getStartingSequece().getCreationDate()); Assert.assertEquals(3, bucket.getStartingSequece().getNumber()); bucket.stop(); } catch (StorageClosedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("*************************************************************"); System.out.println("****************************End******************************"); System.out.println("*************************************************************"); }