lock test & pipeline

This commit is contained in:
Niccolo Borgioli 2023-07-09 18:08:39 +02:00
parent 7c11ba18fd
commit 79725079f7
No known key found for this signature in database
GPG Key ID: D93C615F75EE4F0B
4 changed files with 57 additions and 8 deletions

17
.github/workflows/build.yaml vendored Normal file
View File

@ -0,0 +1,17 @@
name: Build
on:
push:
branches:
- "2"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- run: bun test
- run: bun bin

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
/autorestic
test

View File

@ -1,7 +1,7 @@
import { describe, expect, mock, test, beforeEach } from 'bun:test'
import { lockRepo } from '.'
import { Context } from '../models/context'
import { beforeEach, describe, expect, test } from 'bun:test'
import { mkdir, rm } from 'node:fs/promises'
import { lockRepo, unlockRepo, waitForRepo } from '.'
import { Context } from '../models/context'
const mockPath = './test/'
const mockContext: Context = { config: { meta: { path: mockPath } } } as any
@ -13,8 +13,39 @@ describe('lock', () => {
await mkdir(mockPath, { recursive: true })
})
test('lock', () => {
lockRepo(mockContext, 'foo')
// lockRepo(mockContext, 'foo')
test('simple lock and unlock', () => {
const repo = 'foo'
lockRepo(mockContext, repo)
unlockRepo(mockContext, repo)
})
test('should not be able to lock twice', () => {
const repo = 'foo'
lockRepo(mockContext, repo)
expect(() => {
lockRepo(mockContext, repo)
}).toThrow()
unlockRepo(mockContext, repo)
lockRepo(mockContext, repo)
})
test('should be able to eventually acquire lock', async () => {
const repo = 'foo'
lockRepo(mockContext, repo)
setTimeout(() => unlockRepo(mockContext, repo), 50)
await waitForRepo(mockContext, repo, 1)
})
test('unlock', () => {
unlockRepo(mockContext, 'foo')
})
test('multiple', () => {
const a = 'foo'
const b = 'bar'
lockRepo(mockContext, a)
lockRepo(mockContext, b)
unlockRepo(mockContext, b)
unlockRepo(mockContext, a)
})
})

View File

@ -51,11 +51,11 @@ export function lockRepo(ctx: Context, repo: string) {
*/
export async function waitForRepo(ctx: Context, repo: string, timeout = 10) {
const now = Date.now()
while (Date.now() - now < timeout * 1_000) {
while (Date.now() - now <= timeout * 1_000) {
try {
lockRepo(ctx, repo)
l.trace('repo is free again', { repo })
break
return
} catch {
l.trace('waiting for repo to be unlocked', { repo })
await wait(0.1) // Wait for 100ms