Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if current user still holds the write lock when upload changes #7000

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f6c1b32
add new tests
jiaruiz717 Jul 22, 2024
7938d6c
specify expire time
jiaruiz717 Jul 23, 2024
c862f8b
add lockExpireSeconds
jiaruiz717 Jul 23, 2024
a9125af
remove lines
jiaruiz717 Jul 23, 2024
607f163
deduct five minutes from expiration time
jiaruiz717 Jul 23, 2024
cd60bb8
remove logger
jiaruiz717 Jul 23, 2024
c79429a
use SQliteDb
jiaruiz717 Jul 24, 2024
ad24375
Merge branch 'master' into jiarui/uploadWhenWriteLockExpired
jiaruiz717 Jul 24, 2024
2aba802
remove sqlite, sqlite3
jiaruiz717 Jul 24, 2024
2ded905
rush lint
jiaruiz717 Jul 24, 2024
1ea5695
use current time
jiaruiz717 Jul 24, 2024
ee5297e
test if write lock is refreshed
jiaruiz717 Jul 26, 2024
0bd6507
test write lock refresh when user still holds it
jiaruiz717 Jul 26, 2024
8a0fdb5
rename functions
jiaruiz717 Jul 29, 2024
8f55fef
remove unused test
jiaruiz717 Jul 29, 2024
3629acd
add a new test case
jiaruiz717 Jul 30, 2024
7ad7593
rename
jiaruiz717 Jul 31, 2024
b6de821
hardcode blockName
jiaruiz717 Aug 12, 2024
1c025b5
Merge branch 'master' into jiarui/uploadWhenWriteLockExpired
nick4598 Oct 1, 2024
95b2f67
make bcv_kv.bcv hardcoded
nick4598 Oct 1, 2024
7f14b76
make bcv_kv.bcv hardcoded part 2
nick4598 Oct 1, 2024
c309296
add comment
nick4598 Oct 1, 2024
b5ae4f2
attempt to clean up tests a bit
nick4598 Oct 9, 2024
280b179
Merge branch 'master' into jiarui/uploadWhenWriteLockExpired
nick4598 Oct 9, 2024
0adb0de
condense two tests into one
nick4598 Oct 9, 2024
3f7927b
add two more write lock tests
nick4598 Oct 10, 2024
c418fa0
Merge branch 'master' into jiarui/uploadWhenWriteLockExpired
nick4598 Oct 14, 2024
f98eec8
build error?
nick4598 Oct 14, 2024
1813706
remove useless console.log
nick4598 Oct 14, 2024
850e6be
remove .only
nick4598 Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions full-stack-tests/backend/src/integration/AzuriteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export namespace AzuriteTest {
return blobClient.exists();
};

export const setWriteLockToExpired = async (container: CloudSqlite.CloudContainer, blockName: string) => {
export const resetWriteLockExpireTime = async (container: CloudSqlite.CloudContainer, blockName: string, numOfMin: number) => {
nick4598 marked this conversation as resolved.
Show resolved Hide resolved
const azClient = createAzClient(container.containerId);
const blobClient = azClient.getBlockBlobClient(blockName);
const tempFilePath = join(KnownLocations.tmpdir, blockName);
Expand All @@ -66,8 +66,8 @@ export namespace AzuriteTest {
bcvDb.withSqliteStatement("SELECT v FROM kv WHERE k = 'writeLock'", (stmt) => {
if (stmt.nextRow()) {
const writeLockData = JSON.parse(stmt.getValueString(0));
const expiresTime = new Date();
expiresTime.setMinutes(expiresTime.getMinutes() - 5);
const expiresTime = new Date(writeLockData.expires);
expiresTime.setMinutes(expiresTime.getMinutes() - numOfMin);
writeLockData.expires = expiresTime.toISOString();
bcvDb.withSqliteStatement("UPDATE kv SET v = ? WHERE k = 'writeLock'", (stmt1) => {
stmt1.bindString(1, JSON.stringify(writeLockData));
Expand All @@ -82,7 +82,7 @@ export namespace AzuriteTest {
fs.unlinkSync(tempFilePath);
};

export const isWriteLockRefreshed = async (container: CloudSqlite.CloudContainer, blockName: string): Promise<boolean> => {
export const isWriteLockRefreshed = async (container: CloudSqlite.CloudContainer, blockName: string, currentTime: Date): Promise<boolean> => {
const azClient = createAzClient(container.containerId);
const blobClient = azClient.getBlockBlobClient(blockName);
const tempFilePath = join(KnownLocations.tmpdir, blockName);
Expand All @@ -94,9 +94,10 @@ export namespace AzuriteTest {
bcvDb.withSqliteStatement("SELECT v FROM kv WHERE k = 'writeLock'", (stmt) => {
if (stmt.nextRow()) {
const writeLockData = JSON.parse(stmt.getValueString(0));
const expiresDate = new Date(writeLockData.expires);
const currentTime = new Date();
resolve(expiresDate > currentTime);
const expiresTime = new Date(writeLockData.expires);
// should be 5 min but let's use 4.5min to avoid time conflict
const writeLockTimeRemains = 4.5*60*1000;
nick4598 marked this conversation as resolved.
Show resolved Hide resolved
resolve(expiresTime >= new Date(currentTime.getTime() + writeLockTimeRemains));
} else {
resolve(false);
}
Expand Down
23 changes: 18 additions & 5 deletions full-stack-tests/backend/src/integration/CloudSqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe("CloudSqlite", () => {
db.saveFileProperty({ name: "logMask", namespace: "logMaskTest", id: 1, subId: 1 }, "this is a test");
db.close();
// set the expires time to five mins earlier, which avoids waiting for 5 mins here.
await azSqlite.setWriteLockToExpired(testContainer1, "bcv_kv.bcv");
await azSqlite.resetWriteLockExpireTime(testContainer1, "bcv_kv.bcv", 5);
await expect(testContainer1.uploadChanges()).to.eventually.be.fulfilled;
MichaelSwigerAtBentley marked this conversation as resolved.
Show resolved Hide resolved
testContainer1.disconnect();
});
Expand All @@ -224,7 +224,7 @@ describe("CloudSqlite", () => {
db1.saveFileProperty({ name: "upload", namespace: "uploadTest", id: 1, subId: 1 }, "this is a test");
db1.close();
// set the expires time to five mins earlier, which avoids waiting for 5 mins here.
await azSqlite.setWriteLockToExpired(testContainer1, "bcv_kv.bcv");
await azSqlite.resetWriteLockExpireTime(testContainer1, "bcv_kv.bcv", 5);
// user2 grabs the write lock
testContainer2.connect(testCache2);
testContainer2.acquireWriteLock(user2);
Expand All @@ -239,15 +239,28 @@ describe("CloudSqlite", () => {
const testContainer1 = testContainers[3];
const testCache1 = azSqlite.makeCaches(["testCache1"])[0];
testContainer1.connect(testCache1);
let currentTime = new Date();
testContainer1.acquireWriteLock(user1);
await testContainer1.copyDatabase("testBim", "testBimCopy1");
const db1 = await BriefcaseDb.open({ fileName: "testBimCopy1", container: testContainer1 });
db1.saveFileProperty({ name: "refresh", namespace: "refreshTest", id: 1, subId: 1 }, "this is a test");
db1.close();
await azSqlite.setWriteLockToExpired(testContainer1, "bcv_kv.bcv");
// there are no other users, uploadChanges should refresh the write lock for the current user.
// case 1: current write lock has expired
// mock 5 min operation time by user1
// write lock is expired after 5 min
await azSqlite.resetWriteLockExpireTime(testContainer1, "bcv_kv.bcv", 5);
// there are no other users, uploadChanges should refresh the write lock for the current user
await testContainer1.uploadChanges();
expect (await azSqlite.isWriteLockRefreshed(testContainer1, "bcv_kv.bcv")).to.be.true;
expect (await azSqlite.isWriteLockRefreshed(testContainer1, "bcv_kv.bcv", currentTime)).to.be.true;
// case 2: current write lock has not expired
currentTime = new Date();
// the write lock is refreshed to 5 minutes later
// mock 4 min operation time by user1
await azSqlite.resetWriteLockExpireTime(testContainer1, "bcv_kv.bcv", 4);
// now user1 only has 1 min to perform further actions
nick4598 marked this conversation as resolved.
Show resolved Hide resolved
// uploadChanges does nothing this time but to refresh write lock
await testContainer1.uploadChanges();
expect (await azSqlite.isWriteLockRefreshed(testContainer1, "bcv_kv.bcv", currentTime)).to.be.true;
testContainer1.disconnect();
});

Expand Down
Loading