Last active
December 18, 2018 21:16
-
-
Save DisposaBoy/4967b04ea5db2b662562e5b2fcfc5d76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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