mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-02 07:05:09 +00:00
Fix addPacket replacing existing and then adding duplicate item
This commit is contained in:
@@ -41,9 +41,15 @@ class DevicePacketQueue {
|
|||||||
private boolean exit = false;
|
private boolean exit = false;
|
||||||
|
|
||||||
DevicePacketQueue(Device device) {
|
DevicePacketQueue(Device device) {
|
||||||
|
this(device, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
DevicePacketQueue(Device device, Boolean startThread) {
|
||||||
mDevice = device;
|
mDevice = device;
|
||||||
|
if (startThread) {
|
||||||
ThreadHelper.execute(new SendingRunnable());
|
ThreadHelper.execute(new SendingRunnable());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a packet (at some point in the future)
|
* Send a packet (at some point in the future)
|
||||||
@@ -56,20 +62,26 @@ class DevicePacketQueue {
|
|||||||
if (exit) {
|
if (exit) {
|
||||||
callback.onFailure(new Exception("Device disconnected!"));
|
callback.onFailure(new Exception("Device disconnected!"));
|
||||||
} else {
|
} else {
|
||||||
|
boolean replaced = false;
|
||||||
|
|
||||||
if (replaceID >= 0) {
|
if (replaceID >= 0) {
|
||||||
final Optional<Item> itemOptional = items.stream()
|
for (Item item : items) {
|
||||||
.filter(item -> item.replaceID == replaceID).findFirst();
|
if (item.replaceID == replaceID) {
|
||||||
itemOptional.ifPresent(item -> {
|
|
||||||
//Replace contents with new contents
|
|
||||||
item.packet = packet;
|
item.packet = packet;
|
||||||
item.callback = callback;
|
item.callback = callback;
|
||||||
});
|
replaced = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!replaced) {
|
||||||
items.addLast(new Item(packet, replaceID, callback));
|
items.addLast(new Item(packet, replaceID, callback));
|
||||||
lock.notify();
|
lock.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if we still have an unsent packet in the queue with the given ID.
|
* Check if we still have an unsent packet in the queue with the given ID.
|
||||||
|
50
tests/org/kde/kdeconnect/DevicePacketQueueTest.java
Normal file
50
tests/org/kde/kdeconnect/DevicePacketQueueTest.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package org.kde.kdeconnect;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
public class DevicePacketQueueTest {
|
||||||
|
@Test
|
||||||
|
public void addPacketWithPositiveReplaceId() {
|
||||||
|
Device device = Mockito.mock(Device.class);
|
||||||
|
Device.SendPacketStatusCallback callback = Mockito.mock(Device.SendPacketStatusCallback.class);
|
||||||
|
|
||||||
|
DevicePacketQueue queue = new DevicePacketQueue(device, false);
|
||||||
|
|
||||||
|
queue.addPacket(new NetworkPacket("Test"), 0, callback);
|
||||||
|
queue.addPacket(new NetworkPacket("Test1"), 1, callback);
|
||||||
|
|
||||||
|
assertNotNull(queue.getAndRemoveUnsentPacket(0));
|
||||||
|
assertNotNull(queue.getAndRemoveUnsentPacket(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addPacketWithNegativeReplaceId() {
|
||||||
|
Device device = Mockito.mock(Device.class);
|
||||||
|
Device.SendPacketStatusCallback callback = Mockito.mock(Device.SendPacketStatusCallback.class);
|
||||||
|
|
||||||
|
DevicePacketQueue queue = new DevicePacketQueue(device, false);
|
||||||
|
|
||||||
|
queue.addPacket(new NetworkPacket("Test"), -1, callback);
|
||||||
|
queue.addPacket(new NetworkPacket("Test1"), -1, callback);
|
||||||
|
|
||||||
|
assertNotNull(queue.getAndRemoveUnsentPacket(-1));
|
||||||
|
assertNotNull(queue.getAndRemoveUnsentPacket(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addPacketReplacesPacket() {
|
||||||
|
Device device = Mockito.mock(Device.class);
|
||||||
|
Device.SendPacketStatusCallback callback = Mockito.mock(Device.SendPacketStatusCallback.class);
|
||||||
|
|
||||||
|
DevicePacketQueue queue = new DevicePacketQueue(device, false);
|
||||||
|
|
||||||
|
queue.addPacket(new NetworkPacket("Test"), 1, callback);
|
||||||
|
queue.addPacket(new NetworkPacket("Test1"), 1, callback);
|
||||||
|
|
||||||
|
assertNotNull(queue.getAndRemoveUnsentPacket(1));
|
||||||
|
assertNull(queue.getAndRemoveUnsentPacket(1));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user