| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // +build darwin dragonfly freebsd linux netbsd openbsd |
| 6 |
| 7 package streamserver |
| 8 |
| 9 import ( |
| 10 "io" |
| 11 "io/ioutil" |
| 12 "net" |
| 13 "os" |
| 14 "path/filepath" |
| 15 "testing" |
| 16 |
| 17 "golang.org/x/net/context" |
| 18 ) |
| 19 |
| 20 type posixNamedPipeTester struct { |
| 21 t *testing.T |
| 22 } |
| 23 |
| 24 // Decorates a test function with a NamedPipeServer serving from a temporary |
| 25 // path. |
| 26 func (npt *posixNamedPipeTester) withServer(f func(i *namedPipeTestInstance)) fu
nc() { |
| 27 return func() { |
| 28 // Create a temporary testing directory. |
| 29 tempdir, err := ioutil.TempDir(os.TempDir(), "named_pipe_posix_t
est") |
| 30 if err != nil { |
| 31 panic("Failed to create testing temporary directory.") |
| 32 } |
| 33 defer func() { |
| 34 // Cleanup the temporary directory. |
| 35 os.RemoveAll(tempdir) |
| 36 }() |
| 37 |
| 38 path := filepath.Join(tempdir, "butler.sock") |
| 39 s := NewNamedPipeServer(context.Background(), path).(*namedPipeS
erver) |
| 40 npt.t.Logf("Created testing server for: %s", path) |
| 41 |
| 42 // Start listening. |
| 43 err = s.Listen() |
| 44 if err != nil { |
| 45 panic("Failed to listen on server instance.") |
| 46 } |
| 47 defer func() { |
| 48 // Close the server. Some of our tests might mess with t
his, so check |
| 49 // first to make sure it's still open. |
| 50 if s.l != nil { |
| 51 s.Close() |
| 52 } |
| 53 }() |
| 54 |
| 55 // goroutine-safe connection creation. |
| 56 inst := &namedPipeTestInstance{ |
| 57 S: s, |
| 58 connect: func() io.WriteCloser { |
| 59 npt.t.Logf("Connecting to testing server at: %s"
, path) |
| 60 conn, err := net.Dial("unix", path) |
| 61 if err != nil { |
| 62 npt.t.Logf("Failed to connect client ins
tance: %s", err) |
| 63 return nil |
| 64 } |
| 65 return conn |
| 66 }, |
| 67 } |
| 68 |
| 69 // Execute the function using this valid server! |
| 70 f(inst) |
| 71 } |
| 72 } |
| 73 |
| 74 func TestNamedPipeServer(t *testing.T) { |
| 75 testNamedPipeServer(t, &posixNamedPipeTester{t}) |
| 76 } |
| OLD | NEW |