udp

package module
v0.0.0-...-469649b Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 5 Imported by: 0

README

go-udp-testing

Build Status

Provides UDP socket test helpers for Go.

Documentation

Examples

package main

import (
  "github.com/stvp/go-udp-testing"
  "testing"
)

func TestStatsdReporting(t *testing.T) {
  udp.SetAddr(":8125")

  udp.ShouldReceiveOnly(t, "mystat:2|g", func() {
    statsd.Gauge("mystat", 2)
  })

  udp.ShouldNotReceiveOnly(t, "mystat:1|c", func() {
    statsd.Gauge("bukkit", 2)
  })

  udp.ShouldReceive(t, "bar:2|g", func() {
    statsd.Gauge("foo", 2)
    statsd.Gauge("bar", 2)
    statsd.Gauge("baz", 2)
  })

  udp.ShouldNotReceive(t, "bar:2|g", func() {
    statsd.Gauge("foo", 2)
    statsd.Gauge("baz", 2)
  })

  expected := []string{
    "bar:2|g",
    "baz:5|g",
  }
  udp.ShouldReceiveAll(t, expected, func() {
    statsd.Gauge("bar", 2)
    statsd.Gauge("baz", 2)
  })

  unexpected := []string{
    "bar",
    "baz",
  }
  udp.ShouldNotReceiveAny(t, unexpected, func() {
    statsd.Gauge("foo", 1)
  })

  expected := []string{ "" }
    "bar:2|g",
    "baz:5|g",
  }
  unexpected := []string{
    "foo",
  }
  udp.ShouldReceiveAllAndNotReceiveAny(t, expected, unexpected, func() {
    statsd.Gauge("bar", 2)
    statsd.Gauge("baz", 5)
  })
}

Documentation

Overview

Package udp implements UDP test helpers. It lets you assert that certain strings must or must not be sent to a given local UDP listener.

Index

Constants

This section is empty.

Variables

View Source
var (
	Timeout = time.Millisecond
)

Functions

func ReceiveString

func ReceiveString(t *testing.T, body fn) string

func RemoteAddr

func RemoteAddr() net.Addr

func SetAddr

func SetAddr(a string)

SetAddr sets the UDP port that will be listened on.

func ShouldNotReceive

func ShouldNotReceive(t *testing.T, expected string, body fn)

ShouldNotReceive will fire a test error if the given function sends the given string over UDP.

func ShouldNotReceiveAny

func ShouldNotReceiveAny(t *testing.T, unexpected []string, body fn)

ShouldNotReceiveAny will fire a test error if any of the given strings are sent over UDP.

func ShouldNotReceiveOnly

func ShouldNotReceiveOnly(t *testing.T, notExpected string, body fn)

ShouldNotReceiveOnly will fire a test error if the given function sends exactly the given string over UDP.

func ShouldReceive

func ShouldReceive(t *testing.T, expected string, body fn)

ShouldReceive will fire a test error if the given function doesn't send the given string over UDP.

func ShouldReceiveAll

func ShouldReceiveAll(t *testing.T, expected []string, body fn)

ShouldReceiveAll will fire a test error unless all of the given strings are sent over UDP.

func ShouldReceiveAllAndNotReceiveAny

func ShouldReceiveAllAndNotReceiveAny(t *testing.T, expected []string, unexpected []string, body fn)

func ShouldReceiveOnly

func ShouldReceiveOnly(t *testing.T, expected string, body fn)

ShouldReceiveOnly will fire a test error if the given function doesn't send exactly the given string over UDP.

func Write

func Write(b []byte) (n int, err error)

func WriteTo

func WriteTo(b []byte, addr net.Addr) (n int, err error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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