Skip to content

Commit

Permalink
Fix test to use disableGC option
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Nov 6, 2023
1 parent 9ecca35 commit 08efd9c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/document/crdt/element_rht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ export class ElementRHT {
return !node.isRemoved();
}

/**
* `hasByCreatedAt` returns whether the element exists of the given createdAt or not.
*/
public hasByCreatedAt(createdAt: TimeTicket): boolean {
const node = this.nodeMapByCreatedAt.get(createdAt.toIDString());
if (node == null) {
return false;
}
return !node.isRemoved();
}

/**
* `get` returns the value of the given key.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/document/crdt/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ export class CRDTObject extends CRDTContainer {
return this.memberNodes.has(key);
}

/**
* `hasByCreatedAt` returns whether the element exists of the given
* createdAt or not.
*/
public hasByCreatedAt(createdAt: TimeTicket): boolean {
return this.memberNodes.hasByCreatedAt(createdAt);
}

/**
* `toJSON` returns the JSON encoding of this object.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/document/operation/remove_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ export class RemoveOperation extends Operation {
logger.fatal(`only object and array can execute remove: ${parentObject}`);
}
const obj = parentObject as CRDTContainer;
const key = obj.subPathOf(this.createdAt);
// NOTE(chacha912): Handle cases where operation cannot be executed during undo and redo.
if (
source === OpSource.UndoRedo &&
(obj.getRemovedAt() ||
(obj instanceof CRDTObject && !obj.has(key!)) ||
(obj instanceof CRDTObject && !obj.hasByCreatedAt(this.createdAt)) ||
(obj instanceof CRDTArray && !obj.get(this.createdAt)))
) {
return;
}
const key = obj.subPathOf(this.createdAt);
const reverseOp = this.getReverseOperation(obj);

const elem = obj.delete(this.createdAt, this.getExecutedAt());
Expand Down
14 changes: 8 additions & 6 deletions test/integration/object_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,16 @@ describe('Object', function () {
assert.equal(doc2.toSortedJSON(), '{"color":"red"}');
});

it.skip(`Should handle case of reverse operations referencing already garbage-collected elements`, async function ({
it(`Should handle case of reverse operations referencing already garbage-collected elements`, async function ({
task,
}) {
interface TestDoc {
shape: { color: string };
}
const docKey = toDocKey(`${task.name}-${new Date().getTime()}`);
const doc1 = new Document<TestDoc>(docKey);
const doc2 = new Document<TestDoc>(docKey);
// TODO(chacha912): Remove the disableGC option
const doc1 = new Document<TestDoc>(docKey, { disableGC: true });
const doc2 = new Document<TestDoc>(docKey, { disableGC: true });

const client1 = new Client(testRPCAddr);
const client2 = new Client(testRPCAddr);
Expand Down Expand Up @@ -626,12 +627,13 @@ describe('Object', function () {
assert.equal(doc1.toSortedJSON(), '{"shape":{"color":"red"}}');
assert.equal(doc2.toSortedJSON(), '{"shape":{"color":"red"}}');

// TODO(chacha912): fix error that occurs when undoing and make the test pass
doc1.history.undo();
assert.equal(doc1.toSortedJSON(), '{}');
assert.equal(doc1.toSortedJSON(), '{"shape":{"color":"red"}}');
assert.equal(doc1.getRedoStackForTest().length, 0);
assert.equal(doc1.history.canRedo(), false);
await client1.sync();
await client2.sync();
assert.equal(doc2.toSortedJSON(), '{}');
assert.equal(doc2.toSortedJSON(), '{"shape":{"color":"red"}}');
});
});
});

0 comments on commit 08efd9c

Please sign in to comment.