lock test & pipeline

2
Niccolo Borgioli 1 year ago
parent 7c11ba18fd
commit 79725079f7
No known key found for this signature in database
GPG Key ID: D93C615F75EE4F0B

@ -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

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

@ -1,7 +1,7 @@
import { describe, expect, mock, test, beforeEach } from 'bun:test' import { beforeEach, describe, expect, test } from 'bun:test'
import { lockRepo } from '.'
import { Context } from '../models/context'
import { mkdir, rm } from 'node:fs/promises' import { mkdir, rm } from 'node:fs/promises'
import { lockRepo, unlockRepo, waitForRepo } from '.'
import { Context } from '../models/context'
const mockPath = './test/' const mockPath = './test/'
const mockContext: Context = { config: { meta: { path: mockPath } } } as any const mockContext: Context = { config: { meta: { path: mockPath } } } as any
@ -13,8 +13,39 @@ describe('lock', () => {
await mkdir(mockPath, { recursive: true }) await mkdir(mockPath, { recursive: true })
}) })
test('lock', () => { test('simple lock and unlock', () => {
lockRepo(mockContext, 'foo') const repo = 'foo'
// lockRepo(mockContext, '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)
}) })
}) })

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

Loading…
Cancel
Save