~james/core

core/spindle/instance_lock_test.go -rw-r--r-- 751 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//go:build unix

package spindle

import (
	"path/filepath"
	"strings"
	"testing"
)

func TestSecondSpindleOnTheSameDatabaseIsRefused(t *testing.T) {
	dbPath := filepath.Join(t.TempDir(), "spindle.db")

	release, err := lockInstance(dbPath)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := lockInstance(dbPath); err == nil || !strings.Contains(err.Error(), "another spindle") {
		t.Fatalf("second lock = %v, want a refusal while the first holds it", err)
	}
	release()

	again, err := lockInstance(dbPath)
	if err != nil {
		t.Fatalf("the lock was not given back: %v", err)
	}
	again()
}

func TestNoDatabasePathMeansNoLock(t *testing.T) {
	for range 2 {
		release, err := lockInstance("")
		if err != nil {
			t.Fatal(err)
		}
		defer release()
	}
}