You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.6 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "flag"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "github.com/syndtr/goleveldb/leveldb/storage"
  11. )
  12. var (
  13. filename string
  14. child bool
  15. )
  16. func init() {
  17. flag.StringVar(&filename, "filename", filepath.Join(os.TempDir(), "goleveldb_filelock_test"), "Filename used for testing")
  18. flag.BoolVar(&child, "child", false, "This is the child")
  19. }
  20. func runChild() error {
  21. var args []string
  22. args = append(args, os.Args[1:]...)
  23. args = append(args, "-child")
  24. cmd := exec.Command(os.Args[0], args...)
  25. var out bytes.Buffer
  26. cmd.Stdout = &out
  27. err := cmd.Run()
  28. r := bufio.NewReader(&out)
  29. for {
  30. line, _, e1 := r.ReadLine()
  31. if e1 != nil {
  32. break
  33. }
  34. fmt.Println("[Child]", string(line))
  35. }
  36. return err
  37. }
  38. func main() {
  39. flag.Parse()
  40. fmt.Printf("Using path: %s\n", filename)
  41. if child {
  42. fmt.Println("Child flag set.")
  43. }
  44. stor, err := storage.OpenFile(filename, false)
  45. if err != nil {
  46. fmt.Printf("Could not open storage: %s", err)
  47. os.Exit(10)
  48. }
  49. if !child {
  50. fmt.Println("Executing child -- first test (expecting error)")
  51. err := runChild()
  52. if err == nil {
  53. fmt.Println("Expecting error from child")
  54. } else if err.Error() != "exit status 10" {
  55. fmt.Println("Got unexpected error from child:", err)
  56. } else {
  57. fmt.Printf("Got error from child: %s (expected)\n", err)
  58. }
  59. }
  60. err = stor.Close()
  61. if err != nil {
  62. fmt.Printf("Error when closing storage: %s", err)
  63. os.Exit(11)
  64. }
  65. if !child {
  66. fmt.Println("Executing child -- second test")
  67. err := runChild()
  68. if err != nil {
  69. fmt.Println("Got unexpected error from child:", err)
  70. }
  71. }
  72. os.RemoveAll(filename)
  73. }