3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Allow shutting down wallet auto-saving.

This commit is contained in:
Andreas Schildbach 2014-03-18 22:43:25 +01:00 committed by Mike Hearn
parent 7ffe2a6360
commit b303d77029
3 changed files with 48 additions and 1 deletions

View File

@ -437,6 +437,25 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
}
}
/**
* <p>
* Disables auto-saving, after it had been enabled with
* {@link Wallet#autosaveToFile(java.io.File, long, java.util.concurrent.TimeUnit, com.google.bitcoin.wallet.WalletFiles.Listener)}
* before. This method blocks until finished.
* </p>
*/
public void shutdownAutosaveAndWait() {
lock.lock();
try {
WalletFiles files = vFileManager;
vFileManager = null;
checkState(files != null, "Auto saving not enabled.");
files.shutdownAndWait();
} finally {
lock.unlock();
}
}
private void saveLater() {
WalletFiles files = vFileManager;
if (files != null)

View File

@ -1,5 +1,6 @@
/**
* Copyright 2013 Google Inc.
* Copyright 2014 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -77,6 +78,7 @@ public class WalletFiles {
this.executor = new ScheduledThreadPoolExecutor(1, builder.build());
this.executor.setKeepAliveTime(5, TimeUnit.SECONDS);
this.executor.allowCoreThreadTimeOut(true);
this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
this.wallet = checkNotNull(wallet);
// File must only be accessed from the auto-save executor from now on, to avoid simultaneous access.
this.file = checkNotNull(file);
@ -132,4 +134,14 @@ public class WalletFiles {
return; // Already pending.
executor.schedule(saver, delay, delayTimeUnit);
}
/** Shut down auto-saving. */
public void shutdownAndWait() {
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); // forever
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
}
}

View File

@ -1,5 +1,6 @@
/**
* Copyright 2011 Google Inc.
* Copyright 2014 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -1231,9 +1232,24 @@ public class WalletTest extends TestWithWallet {
// Wait for an auto-save to occur.
latch.await();
assertFalse(hash4.equals(Sha256Hash.hashFileContents(f))); // File has now changed.
Sha256Hash hash5 = Sha256Hash.hashFileContents(f);
assertFalse(hash4.equals(hash5)); // File has now changed.
assertNotNull(results[0]);
assertEquals(f, results[1]);
// Now we shutdown auto-saving and expect wallet changes to remain unsaved, even "important" changes.
wallet.shutdownAutosaveAndWait();
results[0] = results[1] = null;
ECKey key2 = new ECKey();
wallet.addKey(key2);
assertEquals(hash5, Sha256Hash.hashFileContents(f)); // File has NOT changed.
Transaction t2 = createFakeTx(params, toNanoCoins(5, 0), key2);
Block b3 = createFakeBlock(blockStore, t2).block;
chain.add(b3);
Thread.sleep(2000); // Wait longer than autosave delay. TODO Fix the racyness.
assertEquals(hash5, Sha256Hash.hashFileContents(f)); // File has still NOT changed.
assertNull(results[0]);
assertNull(results[1]);
}
@Test