| 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 package streamserver |
| 6 |
| 7 import ( |
| 8 "io" |
| 9 "io/ioutil" |
| 10 "net" |
| 11 "os" |
| 12 "path/filepath" |
| 13 "testing" |
| 14 |
| 15 "golang.org/x/net/context" |
| 16 ) |
| 17 |
| 18 type posixNamedPipeTester struct { |
| 19 t *testing.T |
| 20 } |
| 21 |
| 22 // Decorates a test function with a NamedPipeServer serving from a temporary |
| 23 // path. |
| 24 func (npt *posixNamedPipeTester) withServer(f func(i *namedPipeTestInstance)) fu
nc() { |
| 25 return func() { |
| 26 // Create a temporary testing directory. |
| 27 tempdir, err := ioutil.TempDir(os.TempDir(), "npipe_posix_test") |
| 28 if err != nil { |
| 29 panic("Failed to create testing temporary directory.") |
| 30 } |
| 31 defer func() { |
| 32 // Cleanup the temporary directory. |
| 33 os.RemoveAll(tempdir) |
| 34 }() |
| 35 |
| 36 s := NewNamedPipeServer( |
| 37 context.Background(), |
| 38 filepath.Join(tempdir, "butler.sock"), |
| 39 ).(*namedPipeServer) |
| 40 npt.t.Logf("Created testing server for: %s", 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.serving { |
| 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"
, s.path) |
| 60 conn, err := net.Dial("unix", s.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 |