mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-14 19:25:51 +00:00
SPVBlockStoreTest: Add tests for file locking.
This commit is contained in:
parent
c35d892fa6
commit
dbc4c35209
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013 Google Inc.
|
* Copyright 2013 Google Inc.
|
||||||
|
* Copyright 2018 Andreas Schildbach
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -16,27 +17,33 @@
|
|||||||
|
|
||||||
package org.bitcoinj.store;
|
package org.bitcoinj.store;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
import org.bitcoinj.core.Address;
|
import org.bitcoinj.core.Address;
|
||||||
import org.bitcoinj.core.ECKey;
|
import org.bitcoinj.core.ECKey;
|
||||||
import org.bitcoinj.core.LegacyAddress;
|
import org.bitcoinj.core.LegacyAddress;
|
||||||
import org.bitcoinj.core.NetworkParameters;
|
import org.bitcoinj.core.NetworkParameters;
|
||||||
import org.bitcoinj.core.StoredBlock;
|
import org.bitcoinj.core.StoredBlock;
|
||||||
import org.bitcoinj.params.UnitTestParams;
|
import org.bitcoinj.params.UnitTestParams;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
public class SPVBlockStoreTest {
|
public class SPVBlockStoreTest {
|
||||||
private static final NetworkParameters UNITTEST = UnitTestParams.get();
|
private static final NetworkParameters UNITTEST = UnitTestParams.get();
|
||||||
|
private File blockStoreFile;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws Exception {
|
||||||
|
blockStoreFile = File.createTempFile("spvblockstore", null);
|
||||||
|
blockStoreFile.delete();
|
||||||
|
blockStoreFile.deleteOnExit();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void basics() throws Exception {
|
public void basics() throws Exception {
|
||||||
File f = File.createTempFile("spvblockstore", null);
|
SPVBlockStore store = new SPVBlockStore(UNITTEST, blockStoreFile);
|
||||||
f.delete();
|
|
||||||
f.deleteOnExit();
|
|
||||||
SPVBlockStore store = new SPVBlockStore(UNITTEST, f);
|
|
||||||
|
|
||||||
Address to = LegacyAddress.fromKey(UNITTEST, new ECKey());
|
Address to = LegacyAddress.fromKey(UNITTEST, new ECKey());
|
||||||
// Check the first block in a new store is the genesis block.
|
// Check the first block in a new store is the genesis block.
|
||||||
@ -44,7 +51,6 @@ public class SPVBlockStoreTest {
|
|||||||
assertEquals(UNITTEST.getGenesisBlock(), genesis.getHeader());
|
assertEquals(UNITTEST.getGenesisBlock(), genesis.getHeader());
|
||||||
assertEquals(0, genesis.getHeight());
|
assertEquals(0, genesis.getHeight());
|
||||||
|
|
||||||
|
|
||||||
// Build a new block.
|
// Build a new block.
|
||||||
StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
|
StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
|
||||||
store.put(b1);
|
store.put(b1);
|
||||||
@ -52,11 +58,24 @@ public class SPVBlockStoreTest {
|
|||||||
store.close();
|
store.close();
|
||||||
|
|
||||||
// Check we can get it back out again if we rebuild the store object.
|
// Check we can get it back out again if we rebuild the store object.
|
||||||
store = new SPVBlockStore(UNITTEST, f);
|
store = new SPVBlockStore(UNITTEST, blockStoreFile);
|
||||||
StoredBlock b2 = store.get(b1.getHeader().getHash());
|
StoredBlock b2 = store.get(b1.getHeader().getHash());
|
||||||
assertEquals(b1, b2);
|
assertEquals(b1, b2);
|
||||||
// Check the chain head was stored correctly also.
|
// Check the chain head was stored correctly also.
|
||||||
StoredBlock chainHead = store.getChainHead();
|
StoredBlock chainHead = store.getChainHead();
|
||||||
assertEquals(b1, chainHead);
|
assertEquals(b1, chainHead);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = BlockStoreException.class)
|
||||||
|
public void twoStores_onSameFile() throws Exception {
|
||||||
|
new SPVBlockStore(UNITTEST, blockStoreFile);
|
||||||
|
new SPVBlockStore(UNITTEST, blockStoreFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void twoStores_butSequentially() throws Exception {
|
||||||
|
SPVBlockStore store = new SPVBlockStore(UNITTEST, blockStoreFile);
|
||||||
|
store.close();
|
||||||
|
store = new SPVBlockStore(UNITTEST, blockStoreFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user