netutils

package module
v0.63.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: MIT Imports: 6 Imported by: 1

README

Coverage Status Renovate enabled Build status Build status Go Report Card

Netutils

This is a series of networking utilities and test wrappers by entropy for building robust networked services in golang. See the godoc for details

What can I do with this?

The godoc should cover most of it. I've highlighted a few things below and will add more examples as time goes on. Examples are also present in the godoc

Mocking:

One thing peculiarity of the httmock library is you can't actually pass it a handler. WrapHandler let's you do so:

Handler Mocking:

(see mock_test for a more detailed example)

   package main
   import (
   	"github.com/jarcoal/httpmock"
   )

       func main(){
       httpmock.Activate()
       defer httpmock.Deactivate()
       ctx := context.Background()
       requestCount := 0
       testServer := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
           rw.WriteHeader(200)
       })
   
       httpmock.RegisterResponder("POST", "https://api.github.com/graphql", testutils.WrapHandler(testServer))
}   

There's also a fasthttp module for mocking fasthttp servers/clients with http mock (see tests)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitForConnect

func WaitForConnect(host string) bool

WaitForConnect on a port progressively backing off returns false if we couldn't establish a connection uses default timeout of 5 seconds

Example

WaitForConnect will attempt to connect and timeout after 10 retries of up to 5 seconds

package main

import (
	"fmt"

	"github.com/xplorfin/netutils"
)

func main() {
	// will attempt to connect and not timeout since url is valid
	successful := netutils.WaitForConnect("https://entropy.rocks")
	if successful {
		fmt.Println("connected to entropy!")
	}
}

func WaitForConnectTimeout

func WaitForConnectTimeout(host string, timeout time.Duration) bool

WaitForConnectTimeout will wait for a connection on a port progressively backing off returns false if we couldn't establish a connection by timeout after 10 timeouts

Example

WaitForConnectTimeout will attempt to connect and timeout after 5 seconds

package main

import (
	"fmt"
	"time"

	"github.com/xplorfin/netutils"
)

func main() {
	// will attempt to connect and not timeout since url is valid
	successful := netutils.WaitForConnectTimeout("https://entropy.rocks", 5*time.Second)
	if successful {
		fmt.Println("connected to entropy!")
	}

	successful = netutils.WaitForConnectTimeout("https://entropy.rockssocks", time.Millisecond)
	if !successful {
		fmt.Println("could not connect to non-existent domain")
	}
}

Types

type FreePortStack

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

FreePortStack stack allows you to locally store a list of autogenerated ports it is stateful to make sure ports aren't reused

func NewFreeportStack

func NewFreeportStack() FreePortStack

NewFreeportStack is a helper method to initialize a freeport stack

func (*FreePortStack) GetFreePort

func (s *FreePortStack) GetFreePort() (port int, err error)

GetFreePort gets a free port that has not already been used in the stack returns errors if they exist

Example

Freeport stack gets multiple freeports that are non overlapping

package main

import (
	"github.com/xplorfin/netutils"
	"github.com/xplorfin/netutils/testutils"
)

func main() {
	// create a port stack
	stack := netutils.NewFreeportStack()

	// stack.GetPort will guarantee the ports have not been used before in the stack
	for _, port := range []int{stack.GetPort(), stack.GetPort()} {
		port := port
		go func() {
			err := testutils.MockHTTPServer(port)
			if err != nil {
				panic(err)
			}
		}()
	}
}

func (*FreePortStack) GetPort

func (s *FreePortStack) GetPort() (port int)

GetPort gets a free port that has not already been used in the stack panics on error

Directories

Path Synopsis
Package fasthttp contains fasthttp specific utilities for handling compression, authorization, and more
Package fasthttp contains fasthttp specific utilities for handling compression, authorization, and more
Package testutils contains utilties for mocking various kinds of network interfaces and asserting networked events did or did not happen
Package testutils contains utilties for mocking various kinds of network interfaces and asserting networked events did or did not happen

Jump to

Keyboard shortcuts

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