mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
Merge branch '1455-stream-context-may-be-null' into 'master'
Stream context may be null Closes #1455 See merge request briar/briar!988
This commit is contained in:
@@ -95,7 +95,7 @@ public interface DatabaseComponent {
|
||||
*/
|
||||
@Nullable
|
||||
<R, E extends Exception> R transactionWithNullableResult(boolean readOnly,
|
||||
DbCallable<R, E> task) throws DbException, E;
|
||||
NullableDbCallable<R, E> task) throws DbException, E;
|
||||
|
||||
/**
|
||||
* Stores a contact associated with the given local and remote pseudonyms,
|
||||
|
||||
@@ -2,11 +2,8 @@ package org.briarproject.bramble.api.db;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface DbCallable<R, E extends Exception> {
|
||||
|
||||
@Nullable
|
||||
R call(Transaction txn) throws DbException, E;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.briarproject.bramble.api.db;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface NullableDbCallable<R, E extends Exception> {
|
||||
|
||||
@Nullable
|
||||
R call(Transaction txn) throws DbException, E;
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import org.briarproject.bramble.api.db.NoSuchGroupException;
|
||||
import org.briarproject.bramble.api.db.NoSuchLocalAuthorException;
|
||||
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
||||
import org.briarproject.bramble.api.db.NoSuchTransportException;
|
||||
import org.briarproject.bramble.api.db.NullableDbCallable;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
@@ -183,14 +184,20 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
@Override
|
||||
public <R, E extends Exception> R transactionWithResult(boolean readOnly,
|
||||
DbCallable<R, E> task) throws DbException, E {
|
||||
R result = transactionWithNullableResult(readOnly, task);
|
||||
if (result == null) throw new NullPointerException();
|
||||
return result;
|
||||
Transaction txn = startTransaction(readOnly);
|
||||
try {
|
||||
R result = task.call(txn);
|
||||
commitTransaction(txn);
|
||||
return result;
|
||||
} finally {
|
||||
endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, E extends Exception> R transactionWithNullableResult(
|
||||
boolean readOnly, DbCallable<R, E> task) throws DbException, E {
|
||||
boolean readOnly, NullableDbCallable<R, E> task)
|
||||
throws DbException, E {
|
||||
Transaction txn = startTransaction(readOnly);
|
||||
try {
|
||||
R result = task.call(txn);
|
||||
|
||||
@@ -137,7 +137,7 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
||||
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
|
||||
return null;
|
||||
}
|
||||
return db.transactionWithResult(false, txn ->
|
||||
return db.transactionWithNullableResult(false, txn ->
|
||||
m.getStreamContext(txn, c));
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ class KeyManagerImpl implements KeyManager, Service, EventListener {
|
||||
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
|
||||
return null;
|
||||
}
|
||||
return db.transactionWithResult(false, txn ->
|
||||
return db.transactionWithNullableResult(false, txn ->
|
||||
m.getStreamContext(txn, tag));
|
||||
}
|
||||
|
||||
|
||||
@@ -50,12 +50,12 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
|
||||
oneOf(eventBus).addListener(session);
|
||||
// No acks to send
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withDbCallable(noAckTxn));
|
||||
withNullableDbCallable(noAckTxn));
|
||||
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
|
||||
will(returnValue(null));
|
||||
// No messages to send
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withDbCallable(noMsgTxn));
|
||||
withNullableDbCallable(noMsgTxn));
|
||||
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
|
||||
with(any(int.class)), with(MAX_LATENCY));
|
||||
will(returnValue(null));
|
||||
@@ -84,25 +84,25 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
|
||||
oneOf(eventBus).addListener(session);
|
||||
// One ack to send
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withDbCallable(ackTxn));
|
||||
withNullableDbCallable(ackTxn));
|
||||
oneOf(db).generateAck(ackTxn, contactId, MAX_MESSAGE_IDS);
|
||||
will(returnValue(ack));
|
||||
oneOf(recordWriter).writeAck(ack);
|
||||
// One message to send
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withDbCallable(msgTxn));
|
||||
withNullableDbCallable(msgTxn));
|
||||
oneOf(db).generateBatch(with(msgTxn), with(contactId),
|
||||
with(any(int.class)), with(MAX_LATENCY));
|
||||
will(returnValue(singletonList(message)));
|
||||
oneOf(recordWriter).writeMessage(message);
|
||||
// No more acks
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withDbCallable(noAckTxn));
|
||||
withNullableDbCallable(noAckTxn));
|
||||
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
|
||||
will(returnValue(null));
|
||||
// No more messages
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withDbCallable(noMsgTxn));
|
||||
withNullableDbCallable(noMsgTxn));
|
||||
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
|
||||
with(any(int.class)), with(MAX_LATENCY));
|
||||
will(returnValue(null));
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbCallable;
|
||||
import org.briarproject.bramble.api.db.DbRunnable;
|
||||
import org.briarproject.bramble.api.db.NullableDbCallable;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.jmock.Expectations;
|
||||
|
||||
@@ -21,4 +22,12 @@ public class DbExpectations extends Expectations {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected <R, E extends Exception> NullableDbCallable<R, E> withNullableDbCallable(
|
||||
Transaction txn) {
|
||||
addParameterMatcher(any(NullableDbCallable.class));
|
||||
currentBuilder().setAction(
|
||||
new RunTransactionWithNullableResultAction(txn));
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.api.db.NullableDbCallable;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.hamcrest.Description;
|
||||
import org.jmock.api.Action;
|
||||
import org.jmock.api.Invocation;
|
||||
|
||||
public class RunTransactionWithNullableResultAction implements Action {
|
||||
|
||||
private final Transaction txn;
|
||||
|
||||
public RunTransactionWithNullableResultAction(Transaction txn) {
|
||||
this.txn = txn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(Invocation invocation) throws Throwable {
|
||||
NullableDbCallable task =
|
||||
(NullableDbCallable) invocation.getParameter(1);
|
||||
return task.call(txn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("runs a task inside a database transaction");
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testGetStreamContextForContact() throws Exception {
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(false), withDbCallable(txn));
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withNullableDbCallable(txn));
|
||||
oneOf(transportKeyManager).getStreamContext(txn, contactId);
|
||||
will(returnValue(streamContext));
|
||||
}});
|
||||
@@ -149,7 +150,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testGetStreamContextForTag() throws Exception {
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(false), withDbCallable(txn));
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withNullableDbCallable(txn));
|
||||
oneOf(transportKeyManager).getStreamContext(txn, tag);
|
||||
will(returnValue(streamContext));
|
||||
}});
|
||||
@@ -177,7 +179,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
||||
new ContactStatusChangedEvent(inactiveContactId, true);
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(false), withDbCallable(txn));
|
||||
oneOf(db).transactionWithNullableResult(with(false),
|
||||
withNullableDbCallable(txn));
|
||||
oneOf(transportKeyManager).getStreamContext(txn, inactiveContactId);
|
||||
will(returnValue(streamContext));
|
||||
}});
|
||||
|
||||
Reference in New Issue
Block a user