mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-01-02 05:16:25 +00:00
lock test & pipeline
This commit is contained in:
parent
7c11ba18fd
commit
79725079f7
17
.github/workflows/build.yaml
vendored
Normal file
17
.github/workflows/build.yaml
vendored
Normal 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
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
node_modules
|
||||
/autorestic
|
||||
test
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user