Skip to content

Instantly share code, notes, and snippets.

@DisposaBoy
Last active December 18, 2018 21:16
package main
import (
"flag"
"fmt"
"github.com/mholt/certmagic"
"log"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
)
var (
fs = &certmagic.FileStorage{Path: filepath.Join(os.TempDir(), "certmagic-test")}
)
func main() {
name := flag.String("name", "parent", "")
crash := flag.Bool("crash", false, "")
flag.Parse()
wg := &sync.WaitGroup{}
if *name == "parent" {
for i := 1; i <= 2; i++ {
wg.Add(1)
go spawn(wg, fmt.Sprintf("child/%d", i))
}
}
for i := 1; i <= 2; i++ {
wg.Add(1)
go lock(wg, fmt.Sprint(*name, "/", i))
}
if *crash {
time.Sleep(100 * time.Millisecond)
log.Fatal(*name, "crashed...")
}
wg.Wait()
}
func spawn(wg *sync.WaitGroup, name string) {
defer wg.Done()
exe, err := os.Executable()
if err != nil {
log.Fatalln("Executable:", err)
}
cmd := exec.Command(exe, "-name", name)
cmd.Stderr = os.Stderr
cmd.Run()
}
func lock(wg *sync.WaitGroup, name string) {
defer wg.Done()
k := "hello/world"
log.Println(name, "lock")
err := fs.Lock(k)
if err != nil {
log.Fatalln(name, "Lock:", err)
}
defer func() { log.Println(name, "unlock", fs.Unlock(k)) }()
log.Println(name, "locked...")
time.Sleep(100 * time.Millisecond)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment