mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +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
|
@Nullable
|
||||||
<R, E extends Exception> R transactionWithNullableResult(boolean readOnly,
|
<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,
|
* 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 org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface DbCallable<R, E extends Exception> {
|
public interface DbCallable<R, E extends Exception> {
|
||||||
|
|
||||||
@Nullable
|
|
||||||
R call(Transaction txn) throws DbException, E;
|
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.NoSuchLocalAuthorException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
||||||
import org.briarproject.bramble.api.db.NoSuchTransportException;
|
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.db.Transaction;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
@@ -183,14 +184,20 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
@Override
|
@Override
|
||||||
public <R, E extends Exception> R transactionWithResult(boolean readOnly,
|
public <R, E extends Exception> R transactionWithResult(boolean readOnly,
|
||||||
DbCallable<R, E> task) throws DbException, E {
|
DbCallable<R, E> task) throws DbException, E {
|
||||||
R result = transactionWithNullableResult(readOnly, task);
|
Transaction txn = startTransaction(readOnly);
|
||||||
if (result == null) throw new NullPointerException();
|
try {
|
||||||
return result;
|
R result = task.call(txn);
|
||||||
|
commitTransaction(txn);
|
||||||
|
return result;
|
||||||
|
} finally {
|
||||||
|
endTransaction(txn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R, E extends Exception> R transactionWithNullableResult(
|
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);
|
Transaction txn = startTransaction(readOnly);
|
||||||
try {
|
try {
|
||||||
R result = task.call(txn);
|
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);
|
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return db.transactionWithResult(false, txn ->
|
return db.transactionWithNullableResult(false, txn ->
|
||||||
m.getStreamContext(txn, c));
|
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);
|
if (LOG.isLoggable(INFO)) LOG.info("No key manager for " + t);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return db.transactionWithResult(false, txn ->
|
return db.transactionWithNullableResult(false, txn ->
|
||||||
m.getStreamContext(txn, tag));
|
m.getStreamContext(txn, tag));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,12 +50,12 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
|
|||||||
oneOf(eventBus).addListener(session);
|
oneOf(eventBus).addListener(session);
|
||||||
// No acks to send
|
// No acks to send
|
||||||
oneOf(db).transactionWithNullableResult(with(false),
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
withDbCallable(noAckTxn));
|
withNullableDbCallable(noAckTxn));
|
||||||
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
|
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
|
||||||
will(returnValue(null));
|
will(returnValue(null));
|
||||||
// No messages to send
|
// No messages to send
|
||||||
oneOf(db).transactionWithNullableResult(with(false),
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
withDbCallable(noMsgTxn));
|
withNullableDbCallable(noMsgTxn));
|
||||||
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
|
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
|
||||||
with(any(int.class)), with(MAX_LATENCY));
|
with(any(int.class)), with(MAX_LATENCY));
|
||||||
will(returnValue(null));
|
will(returnValue(null));
|
||||||
@@ -84,25 +84,25 @@ public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
|
|||||||
oneOf(eventBus).addListener(session);
|
oneOf(eventBus).addListener(session);
|
||||||
// One ack to send
|
// One ack to send
|
||||||
oneOf(db).transactionWithNullableResult(with(false),
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
withDbCallable(ackTxn));
|
withNullableDbCallable(ackTxn));
|
||||||
oneOf(db).generateAck(ackTxn, contactId, MAX_MESSAGE_IDS);
|
oneOf(db).generateAck(ackTxn, contactId, MAX_MESSAGE_IDS);
|
||||||
will(returnValue(ack));
|
will(returnValue(ack));
|
||||||
oneOf(recordWriter).writeAck(ack);
|
oneOf(recordWriter).writeAck(ack);
|
||||||
// One message to send
|
// One message to send
|
||||||
oneOf(db).transactionWithNullableResult(with(false),
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
withDbCallable(msgTxn));
|
withNullableDbCallable(msgTxn));
|
||||||
oneOf(db).generateBatch(with(msgTxn), with(contactId),
|
oneOf(db).generateBatch(with(msgTxn), with(contactId),
|
||||||
with(any(int.class)), with(MAX_LATENCY));
|
with(any(int.class)), with(MAX_LATENCY));
|
||||||
will(returnValue(singletonList(message)));
|
will(returnValue(singletonList(message)));
|
||||||
oneOf(recordWriter).writeMessage(message);
|
oneOf(recordWriter).writeMessage(message);
|
||||||
// No more acks
|
// No more acks
|
||||||
oneOf(db).transactionWithNullableResult(with(false),
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
withDbCallable(noAckTxn));
|
withNullableDbCallable(noAckTxn));
|
||||||
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
|
oneOf(db).generateAck(noAckTxn, contactId, MAX_MESSAGE_IDS);
|
||||||
will(returnValue(null));
|
will(returnValue(null));
|
||||||
// No more messages
|
// No more messages
|
||||||
oneOf(db).transactionWithNullableResult(with(false),
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
withDbCallable(noMsgTxn));
|
withNullableDbCallable(noMsgTxn));
|
||||||
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
|
oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
|
||||||
with(any(int.class)), with(MAX_LATENCY));
|
with(any(int.class)), with(MAX_LATENCY));
|
||||||
will(returnValue(null));
|
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.DbCallable;
|
||||||
import org.briarproject.bramble.api.db.DbRunnable;
|
import org.briarproject.bramble.api.db.DbRunnable;
|
||||||
|
import org.briarproject.bramble.api.db.NullableDbCallable;
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
|
|
||||||
@@ -21,4 +22,12 @@ public class DbExpectations extends Expectations {
|
|||||||
return null;
|
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
|
@Test
|
||||||
public void testGetStreamContextForContact() throws Exception {
|
public void testGetStreamContextForContact() throws Exception {
|
||||||
context.checking(new DbExpectations() {{
|
context.checking(new DbExpectations() {{
|
||||||
oneOf(db).transactionWithResult(with(false), withDbCallable(txn));
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
|
withNullableDbCallable(txn));
|
||||||
oneOf(transportKeyManager).getStreamContext(txn, contactId);
|
oneOf(transportKeyManager).getStreamContext(txn, contactId);
|
||||||
will(returnValue(streamContext));
|
will(returnValue(streamContext));
|
||||||
}});
|
}});
|
||||||
@@ -149,7 +150,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetStreamContextForTag() throws Exception {
|
public void testGetStreamContextForTag() throws Exception {
|
||||||
context.checking(new DbExpectations() {{
|
context.checking(new DbExpectations() {{
|
||||||
oneOf(db).transactionWithResult(with(false), withDbCallable(txn));
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
|
withNullableDbCallable(txn));
|
||||||
oneOf(transportKeyManager).getStreamContext(txn, tag);
|
oneOf(transportKeyManager).getStreamContext(txn, tag);
|
||||||
will(returnValue(streamContext));
|
will(returnValue(streamContext));
|
||||||
}});
|
}});
|
||||||
@@ -177,7 +179,8 @@ public class KeyManagerImplTest extends BrambleMockTestCase {
|
|||||||
new ContactStatusChangedEvent(inactiveContactId, true);
|
new ContactStatusChangedEvent(inactiveContactId, true);
|
||||||
|
|
||||||
context.checking(new DbExpectations() {{
|
context.checking(new DbExpectations() {{
|
||||||
oneOf(db).transactionWithResult(with(false), withDbCallable(txn));
|
oneOf(db).transactionWithNullableResult(with(false),
|
||||||
|
withNullableDbCallable(txn));
|
||||||
oneOf(transportKeyManager).getStreamContext(txn, inactiveContactId);
|
oneOf(transportKeyManager).getStreamContext(txn, inactiveContactId);
|
||||||
will(returnValue(streamContext));
|
will(returnValue(streamContext));
|
||||||
}});
|
}});
|
||||||
|
|||||||
Reference in New Issue
Block a user