fix(java): fix (most) unit tests
Some checks failed
test / build (java, macos-latest, beta) (push) Has been cancelled
test / build (java, macos-latest, stable) (push) Has been cancelled
test / build (java, ubuntu-latest, beta) (push) Has been cancelled
test / build (java, ubuntu-latest, stable) (push) Has been cancelled
test / build (java, windows-latest, beta) (push) Has been cancelled
test / build (java, windows-latest, stable) (push) Has been cancelled
test / build (js, macos-latest, beta) (push) Has been cancelled
test / build (js, macos-latest, stable) (push) Has been cancelled
test / build (js, ubuntu-latest, beta) (push) Has been cancelled
test / build (js, ubuntu-latest, stable) (push) Has been cancelled
test / build (js, windows-latest, beta) (push) Has been cancelled
test / build (js, windows-latest, stable) (push) Has been cancelled
test / build (lua54, macos-latest, beta) (push) Has been cancelled
test / build (lua54, macos-latest, stable) (push) Has been cancelled
test / build (lua54, ubuntu-latest, beta) (push) Has been cancelled
test / build (lua54, ubuntu-latest, stable) (push) Has been cancelled
test / build (lua54, windows-latest, beta) (push) Has been cancelled
test / build (lua54, windows-latest, stable) (push) Has been cancelled
test / build (luajit, macos-latest, beta) (push) Has been cancelled
test / build (luajit, macos-latest, stable) (push) Has been cancelled
test / build (luajit, ubuntu-latest, beta) (push) Has been cancelled
test / build (luajit, ubuntu-latest, stable) (push) Has been cancelled
test / build (luajit, windows-latest, beta) (push) Has been cancelled
test / build (luajit, windows-latest, stable) (push) Has been cancelled
test / build (py, macos-latest, beta) (push) Has been cancelled
test / build (py, macos-latest, stable) (push) Has been cancelled
test / build (py, ubuntu-latest, beta) (push) Has been cancelled
test / build (py, windows-latest, stable) (push) Has been cancelled
test / test (beta) (push) Has been cancelled
test / test (stable) (push) Has been cancelled
test / build (py, ubuntu-latest, stable) (push) Has been cancelled
test / build (py, windows-latest, beta) (push) Has been cancelled

However, as mentioned in the text for the PR, we'll be ditching these
to work on the Rust ones. I'm committing what I had here to not lose it,
but these will probably be rustified.
This commit is contained in:
zaaarf 2024-10-16 03:28:15 +02:00 committed by alemi.dev
parent 39b299bbe3
commit 1359e30b44

View file

@ -6,6 +6,7 @@ import mp.code.data.User;
import mp.code.exceptions.ConnectionException; import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException; import mp.code.exceptions.ConnectionRemoteException;
import mp.code.exceptions.ControllerException; import mp.code.exceptions.ControllerException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Objects; import java.util.Objects;
@ -15,15 +16,15 @@ import java.util.UUID;
import static mp.code.data.DetachResult.DETACHING; import static mp.code.data.DetachResult.DETACHING;
@SuppressWarnings({"StatementWithEmptyBody", "OptionalGetWithoutIsPresent"})
public class CodeMPTest { public class CodeMPTest {
private final Client client; private final Client client;
private final Client otherClient; private final Client otherClient;
// client connection init // client connection init
public CodeMPTest() throws ConnectionException { public CodeMPTest() throws ConnectionException {
System.out.println("aaaa"); new Thread(() -> Extensions.drive(true)); // drive thread so callback works
//new Thread(() -> Extensions.drive(true)); // drive thread so callback works //Extensions.setupTracing(null, true);
Extensions.setupTracing(null, true);
this.client = Client.connect(new Config( this.client = Client.connect(new Config(
Objects.requireNonNull(System.getenv("CODEMP_TEST_USERNAME_1")), Objects.requireNonNull(System.getenv("CODEMP_TEST_USERNAME_1")),
@ -33,6 +34,11 @@ public class CodeMPTest {
false false
)); ));
// failed tests may have cluttered the list, clean it first
for(String ws : this.client.listWorkspaces(true, false)) {
this.client.deleteWorkspace(ws);
}
this.otherClient = Client.connect(new Config( this.otherClient = Client.connect(new Config(
Objects.requireNonNull(System.getenv("CODEMP_TEST_USERNAME_2")), Objects.requireNonNull(System.getenv("CODEMP_TEST_USERNAME_2")),
Objects.requireNonNull(System.getenv("CODEMP_TEST_PASSWORD_2")), Objects.requireNonNull(System.getenv("CODEMP_TEST_PASSWORD_2")),
@ -45,12 +51,12 @@ public class CodeMPTest {
@Test @Test
void testGetUser() { void testGetUser() {
User u = this.client.getUser(); User u = this.client.getUser();
System.out.println("User name:" + u.name); //assert u.name.equals(System.getenv("CODEMP_TEST_USERNAME_1"));
System.out.println("User ID: " + u.id); //assert u.id.toString().equals(System.getenv("CODEMP_TEST_ID_1"));
} }
@Test @Test
void testWorkspaceInteraction() throws ConnectionException { void testWorkspaceInteractions() throws ConnectionException {
String randomName = UUID.randomUUID().toString(); String randomName = UUID.randomUUID().toString();
int oldOwned = this.client.listWorkspaces(true, false).length; int oldOwned = this.client.listWorkspaces(true, false).length;
@ -71,7 +77,7 @@ public class CodeMPTest {
this.client.inviteToWorkspace(randomName, this.otherClient.getUser().name); this.client.inviteToWorkspace(randomName, this.otherClient.getUser().name);
assert this.client.leaveWorkspace(randomName); assert this.client.leaveWorkspace(randomName);
assert this.otherClient.leaveWorkspace(randomName); assert !this.otherClient.leaveWorkspace(randomName);
this.client.deleteWorkspace(randomName); this.client.deleteWorkspace(randomName);
} }
@ -85,155 +91,136 @@ public class CodeMPTest {
void testBufferInteractions() throws ConnectionException, ControllerException, InterruptedException { void testBufferInteractions() throws ConnectionException, ControllerException, InterruptedException {
String randomWorkspace = UUID.randomUUID().toString(); String randomWorkspace = UUID.randomUUID().toString();
String randomBuffer = UUID.randomUUID().toString(); String randomBuffer = UUID.randomUUID().toString();
// prepare first client
this.client.createWorkspace(randomWorkspace); this.client.createWorkspace(randomWorkspace);
Workspace ws = this.client.joinWorkspace(randomWorkspace); Workspace ws = this.client.joinWorkspace(randomWorkspace);
this.client.inviteToWorkspace(ws.getWorkspaceId(), this.otherClient.getUser().name); // test buffer creation and verify that the buffer list has changed
int oldFileTree = ws.getFileTree(Optional.empty(), true).length; int oldFileTree = ws.getFileTree(Optional.empty(), true).length;
ws.createBuffer(randomBuffer); ws.createBuffer(randomBuffer);
assert (oldFileTree + 1) == ws.getFileTree(Optional.empty(), true).length; assert (oldFileTree + 1) == ws.getFileTree(Optional.empty(), true).length;
// test buffer filters
assert ws.getFileTree(Optional.of(randomBuffer.substring(0, 10)), true).length == 0; assert ws.getFileTree(Optional.of(randomBuffer.substring(0, 10)), true).length == 0;
assert ws.getFileTree(Optional.of(randomBuffer.substring(0, 10)), false).length == 1; assert ws.getFileTree(Optional.of(randomBuffer.substring(0, 10)), false).length == 1;
ws.deleteBuffer(randomBuffer);
assert oldFileTree == ws.getFileTree(Optional.empty(), true).length;
ws.createBuffer(randomBuffer);
int oldActive = ws.activeBuffers().length; int oldActive = ws.activeBuffers().length;
ws.attachToBuffer(randomBuffer); ws.attachToBuffer(randomBuffer);
assert (oldActive + 1) == ws.activeBuffers().length; assert (oldActive + 1) == ws.activeBuffers().length;
Optional<BufferController> buffer = ws.getBuffer(randomBuffer); BufferController buffer = ws.getBuffer(randomBuffer).get();
assert buffer.isPresent();
buffer.get().callback(bufferController -> { // prepare second client and clean queue
assert true; this.client.inviteToWorkspace(ws.getWorkspaceId(), this.otherClient.getUser().name);
}); BufferController otherBuffer = this.otherClient.joinWorkspace(randomWorkspace).attachToBuffer(randomBuffer);
while(buffer.tryRecv().isPresent()) {}
Thread t = new Thread(() -> parallelBufferThreadTask(randomWorkspace, randomBuffer)); TextChange textChange = new TextChange(0, 0, "", OptionalLong.empty());
t.start();
// wait for other thread to attach /* Testing callback */
while(ws.listBufferUsers(randomBuffer).length == 1) { buffer.callback(bufferController -> new Thread(() -> {
wait(50); try {
} assert bufferController.recv().equals(textChange);
} catch(ControllerException e) {
throw new RuntimeException(e);
}
}).start());
buffer.get().poll(); otherBuffer.send(textChange);
buffer.get().clearCallback(); buffer.poll();
buffer.clearCallback();
buffer.get().recv(); otherBuffer.send(textChange);
buffer.recv();
buffer.get().poll(); otherBuffer.send(textChange);
assert buffer.get().tryRecv().isPresent(); buffer.poll();
assert buffer.tryRecv().isPresent();
assert buffer.get().tryRecv().isEmpty(); assert buffer.tryRecv().isEmpty();
buffer.get().send(new TextChange(0, 0, "1", OptionalLong.empty()));
assert ws.detachFromBuffer(randomBuffer) == DETACHING; assert ws.detachFromBuffer(randomBuffer) == DETACHING;
ws.deleteBuffer(randomBuffer);
this.otherClient.getWorkspace(randomWorkspace).get().createBuffer(UUID.randomUUID().toString());
assert ws.event().getChangedBuffer().isPresent(); assert ws.event().getChangedBuffer().isPresent();
t.join(1000);
ws.deleteBuffer(randomBuffer);
Assertions.assertEquals(oldFileTree, ws.getFileTree(Optional.empty(), true).length);
this.client.leaveWorkspace(randomWorkspace); this.client.leaveWorkspace(randomWorkspace);
this.client.deleteWorkspace(randomWorkspace); this.client.deleteWorkspace(randomWorkspace);
} }
private void parallelBufferThreadTask(String workspace, String buffer) { @Test
try { void testWorkspaceEvents() throws ConnectionException, ControllerException {
Workspace w = this.otherClient.joinWorkspace(workspace); String randomWorkspace = UUID.randomUUID().toString();
BufferController controller = w.attachToBuffer(buffer);
for(int i = 0; i < 3; i++) {
try {
wait(200);
controller.send(new TextChange(
0, 0, "1", OptionalLong.empty()
));
} catch(InterruptedException e) {
break;
}
}
w.detachFromBuffer(buffer);
String anotherRandomBuffer = UUID.randomUUID().toString(); // prepare first client
w.createBuffer(anotherRandomBuffer); this.client.createWorkspace(randomWorkspace);
w.deleteBuffer(anotherRandomBuffer); Workspace ws = this.client.joinWorkspace(randomWorkspace);
} catch(ConnectionException | ControllerException e) { this.client.inviteToWorkspace(randomWorkspace, this.otherClient.getUser().name);
throw new RuntimeException(e);
} // prepare second client
this.otherClient.joinWorkspace(randomWorkspace).createBuffer(UUID.randomUUID().toString());
// block until event is received
assert ws.event().getChangedBuffer().isPresent();
// cleanup
this.otherClient.leaveWorkspace(randomWorkspace);
this.client.deleteWorkspace(randomWorkspace);
} }
@Test @Test
void testCursorInteractions() throws ConnectionException, InterruptedException, ControllerException { void testCursorInteractions() throws ConnectionException, ControllerException, InterruptedException {
String randomWorkspace = UUID.randomUUID().toString(); String randomWorkspace = UUID.randomUUID().toString();
String randomBuffer = UUID.randomUUID().toString(); String randomBuffer = UUID.randomUUID().toString();
// prepare first client // prepare first client
this.client.createWorkspace(randomWorkspace); this.client.createWorkspace(randomWorkspace);
Workspace ws = this.client.joinWorkspace(randomWorkspace); Workspace ws = this.client.joinWorkspace(randomWorkspace);
this.client.inviteToWorkspace(ws.getWorkspaceId(), this.otherClient.getUser().name);
ws.createBuffer(randomBuffer); ws.createBuffer(randomBuffer);
ws.attachToBuffer(randomBuffer); ws.attachToBuffer(randomBuffer);
CursorController cursor = ws.getCursor(); CursorController cursor = ws.getCursor();
// prepare second client (ignore initial cursor for convenience) // prepare second client and clean queue
this.otherClient.joinWorkspace(randomWorkspace).attachToBuffer(randomBuffer); this.client.inviteToWorkspace(ws.getWorkspaceId(), this.otherClient.getUser().name);
CursorController otherCursor = this.otherClient.joinWorkspace(randomWorkspace).getCursor();
while(cursor.tryRecv().isPresent()) {}
cursor.callback(bufferController -> { Cursor someCursor = new Cursor(
assert true; 0, 0, 0, 0, randomBuffer, this.otherClient.getUser().name
}); );
Thread t = new Thread(() -> parallelCursorThreadTask(randomWorkspace, randomBuffer)); /* Testing callback */
t.start(); cursor.callback(cursorController -> new Thread(() -> {
try {
assert cursorController.recv().equals(someCursor);
} catch(ControllerException e) {
throw new RuntimeException(e);
}
}).start());
// wait for other thread to attach otherCursor.send(someCursor);
while(ws.listBufferUsers(randomBuffer).length == 1) { cursor.poll(); // wait for other thread to send
wait(50); cursor.clearCallback(); // should have now received the first callback, clear it
}
/* Testing recv and tryRecv */
otherCursor.send(someCursor);
cursor.recv(); // block until receive
// send flat cursor
otherCursor.send(new Cursor(
0, 0, 0, 0, randomBuffer, this.otherClient.getUser().name
));
cursor.poll(); cursor.poll();
cursor.clearCallback(); assert cursor.tryRecv().isPresent(); // expect something (and consume)
assert cursor.tryRecv().isEmpty(); // expect nothing
cursor.recv();
cursor.poll();
assert cursor.tryRecv().isPresent();
assert cursor.tryRecv().isEmpty();
cursor.send(new Cursor(0, 0, 0, 0, randomBuffer, this.client.getUser().name));
assert ws.detachFromBuffer(randomBuffer) == DETACHING;
ws.deleteBuffer(randomBuffer);
t.join(1000);
// cleanup
this.otherClient.leaveWorkspace(randomWorkspace);
this.client.leaveWorkspace(randomWorkspace); this.client.leaveWorkspace(randomWorkspace);
this.client.deleteWorkspace(randomWorkspace); this.client.deleteWorkspace(randomWorkspace);
}
private void parallelCursorThreadTask(String workspace, String buffer) {
try {
@SuppressWarnings("OptionalGetWithoutIsPresent")
Workspace w = this.otherClient.getWorkspace(workspace).get();
for(int i = 0; i < 3; i++) {
try {
wait(200);
w.getCursor().send(new Cursor(
0, 0, 0, 0, buffer, this.otherClient.getUser().name
));
} catch(InterruptedException e) {
break;
}
}
w.detachFromBuffer(buffer);
} catch(ControllerException e) {
throw new RuntimeException(e);
}
} }
} }