tcptest

package module
v0.0.0-...-0f9f998 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2018 License: MIT Imports: 2 Imported by: 0

README

go-tcptest

Start A Network Server On Random Local Port (Port of Perl5's Test::TCP)

WARNING

This repository has been moved to github.com/lestrrat-go/tcptest. This repository exists so that libraries pointing to this URL will keep functioning, but this repository will NOT be updated in the future. Please use the new import path.

  var cmd *exec.Cmd
  memd := func(port int) {
    cmd = exec.Command("memcached", "-p", fmt.Sprintf("%d", port))
    cmd.SysProcAttr = &syscall.SysProcAttr {
      Setpgid: true,
    }
    cmd.Run()
  }

  server, err := Start(memd, 30 * time.Second)
  if err != nil {
    log.Fatalf("Failed to start memcached: %s", err)
  }

  log.Printf("memcached started on port %d", server.Port())
  defer func() {
    if cmd != nil && cmd.Process != nil {
      cmd.Process.Signal(syscall.SIGTERM)
    }
  }()

  // Do what you want to do with memcached

  // Then when you're done, you need to kill it
  cmd.Process.Signal(syscall.SIGTERM)

  // And wait
  server.Wait()

API docs: http://godoc.org/github.com/lestrrat/go-tcptest

Documentation

Overview

Example
var cmd *exec.Cmd
memd := func(t *TCPTest) {
	cmd = exec.Command("memcached", "-p", fmt.Sprintf("%d", t.Port()))
	cmd.SysProcAttr = &syscall.SysProcAttr{
		Setpgid: true,
	}

	go cmd.Run()
	for loop := true; loop; {
		select {
		case <-t.Done():
			cmd.Process.Kill()
			loop = false
		}
	}
}

server, err := Start2(memd, 30*time.Second)
if err != nil {
	log.Fatalf("Failed to start memcached: %s", err)
}

log.Printf("memcached started on port %d", server.Port())
defer func() {
	if cmd != nil && cmd.Process != nil {
		cmd.Process.Signal(syscall.SIGTERM)
	}
}()

// Do what you want...

// When you're done, you need to kill the daemon
// because, well, it doesn't unless you tell it to!
server.Stop()

// Wait for the callback goroutine to exit
server.Wait()
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TCPTest

type TCPTest struct {
	// contains filtered or unexported fields
}

func Start

func Start(cb func(int), dur time.Duration) (*TCPTest, error)

Start is left for backward compatibility, but you should use Start2(), which allows better management of the lifecycle

func Start2

func Start2(cb func(*TCPTest), dur time.Duration) (*TCPTest, error)

func (*TCPTest) Done

func (t *TCPTest) Done() <-chan struct{}

func (*TCPTest) Port

func (t *TCPTest) Port() int

func (*TCPTest) Stop

func (t *TCPTest) Stop()

Stop sends a notification through t.Done() that the server should be stopped. It's up to the provider of the callback function to properly exit and clean the server

func (*TCPTest) Wait

func (t *TCPTest) Wait()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL