Code Monkey home page Code Monkey logo

pkg's People

Contributors

kamilsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pkg's Issues

errors classifier

classifier := errors.Classifier{
  "some": []error{x, y, z}
}
class := classifier.Classify(err)
metrics.Count(class).Send()

improve classifier

  • add MessageError matcher
  • improve tests
  • improve ClassifyAs
  • control code coverage

add MustReturn function

import "github.com/Masterminds/semver"

var (
	min := fn.MustReturn(semver.NewConstraint(">= 92.0")).(*semver.Constraints)
)

review signals for trap

os.Interrupt, syscall.SIGINT, syscall.SIGTERM -> syscall.SIGINT, syscall.SIGTERM cause os.Interrupt is an alias for syscall.SIGINT

add env package

  • Variable struct { Key, Value }
  • implement Stringer
  • Vars as []Variable
  • Vars.Environ()
  • consts

discard before close

safe.Close(resp.Body, ...) works, but not well. It will be better use something like this:

defer safe.Close(io.Discard(resp.Body), ...)

testing: env: write test to reproduce errors with concurrent access to environment variables

pkg/testing/env.go

Lines 11 to 45 in 19e671e

// SetEnvs allows changing environment variables concurrently.
//
// import(
// "os"
// "testing"
//
// "github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/require"
//
// "go.octolab.org/env"
// . "go.octolab.org/testing"
// )
//
// func Test(t *testing.T) {
// t.Run("case 1", func(t *testing.T) {
// t.Parallel()
//
// release, err := SetEnvs(NoError(t), env.Must(env.GoTraceback, "system"))
// require.NoError(t, err)
//
// assert.Equal(t, "system", os.Getenv(env.GoTraceback))
// release(StrictNoError(t))
// })
//
// t.Run("case 2", func(t *testing.T) {
// t.Parallel()
//
// release, err := SetEnvs(NoError(t), env.Must(env.GoTraceback, "crash"))
// require.NoError(t, err)
//
// assert.Equal(t, "crash", os.Getenv(env.GoTraceback))
// release(StrictNoError(t))
// })
// }
//

request builder for testing

func NewRequest(options ...func(*http.Request)) *http.Request {
	req := new(http.Request)
	for _, configure := range options {
		configure(req)
	}
	return req
}

func WithForm(kv ...string) func(*http.Request) {
	return func(req *http.Request) {
		req.Form = make(url.Values, len(kv))

		for i := 0; i+1 < len(kv); i += 2 {
			req.Form.Set(kv[i], kv[i+1])
		}
	}
}

func WithHeaders(kv ...string) func(*http.Request) {
	return func(req *http.Request) {
		req.Header = make(http.Header, len(kv))

		for i := 0; i+1 < len(kv); i += 2 {
			req.Header.Set(kv[i], kv[i+1])
		}
	}
}

func WithPath(path string) func(*http.Request) {
	return func(req *http.Request) {
		req.URL = new(url.URL)
		req.URL.Path = path
	}
}

new package sequence/generator

The concept:

sequence := new(integer.generator).Since(10).Rhythm(func (int64) int64).Until(10^5)

for range sequence.Source() { ... } // math.MaxInt64 by default

sequence.Next() bool

sequence.Current() int64

sequence.FastForward(int64) bool
uid := new(uuid.generator)

uid.Next() string ?
random := new(rand.generator)

random.Next() ?

extend runtime.Caller

  • add tests with
    • struct
    • fn
    • pointer and not
  • add more shortcuts
    • caller.Package
    • caller.Method
    • caller.Object

add ReadCloser to testing

RepeatableReadCloser(
    helper.ReadCloser("file:testdata/some.data"),
    bytes.NewBuffer(make([]byte, 0, 5)),
)

add async.DelayedJob

job := async.NewDelayedJob(make([]func(), 0, 2))

if cond1 {
  job.Do(...)
}

if cond2 {
  job.Do(...)
}

job.Wait()

safe.Background

	wg :=&sync.WaitGroup{}
	wg.Add(len(data))
	for range data {
		go safe.Background(wg, func() { handler.ServeHTTP(rec, &http.Request{}) }, 1)
	}

improve sync.Times

add a possibility to filter unique action

e.g. sync.DoUnique(fingerprinter, action)

motivation: log duplicates is not good

improve io.Discard for defer

if I use io.Discard with defer I have bad dev experience:

resp, err := http.Do(req)
if err {...}
defer safe.Close(io.Discard(resp.Body), ...) // now I can't read from body

json.NewDecoder(resp.Body).Decode() // woops

helper for testing

	ctx := context.Background()
	deadline, has := t.Deadline()
	if has {
		var cancel context.CancelFunc
		ctx, cancel = context.WithDeadline(ctx, deadline.Add(50*time.Millisecond))
		defer cancel()
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.