Skip to content
This repository has been archived by the owner on Jul 2, 2022. It is now read-only.

Commit

Permalink
Add some doco
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronpm committed Sep 8, 2018
1 parent c1f7f1c commit 1701082
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.txt
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ before_install:
- go get -t -v ./...

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic
- go test -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# conn-expect

A simple expect library designed around using net.Conn

[![Code Coverage](https://img.shields.io/codecov/c/github/cameronpm/conn-expect/master.svg?style=flat-square)](https://codecov.io/gh/cameronpm/conn-expect/)
[![GoDoc](https://godoc.org/github.com/cameronpm/conn-expect?status.png)](https://godoc.org/github.com/cameronpm/conn-expect)
[![Build Status](https://travis-ci.org/cameronpm/conn-expect.svg?branch=master)](https://travis-ci.org/cameronpm/conn-expect)
[![Code Coverage](https://codecov.io/gh/cameronpm/conn-expect/branch/master/graph/badge.svg)](https://codecov.io/gh/cameronpm/conn-expect/)

The tests in expect_test.go are sufficient enough to get started.
50 changes: 37 additions & 13 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@ const (
MsgSend MsgType = "send"
)

// Log is used in combination with the Logger method of the Expect type, to log all inbound/outbound network traffic
type Log struct {
Type MsgType
Data string
}

// Batcher is the simple interface all batch records implement
type Batcher interface {
// Invoke
//
// batchIdx is the index of the batch object, starting at 0
Invoke(ctx context.Context, exp *Expect, timeout time.Duration, batchIdx int) error
}

// BRecv allows regexps to be matched on input
type BRecv struct {
// Re is required, it's the regular expression to use for matching
Re *regexp.Regexp
// OnSuccess is optional, the first element is the entire matched pattern,
// the subsequent elements are submatches. Returning an error from here will
// stop the Batch operation and return with an error
OnSuccess func(matched []string) error
Re *regexp.Regexp
}

// Invoke fulfils the Batcher interface
func (b *BRecv) Invoke(ctx context.Context, exp *Expect, timeout time.Duration, batchIdx int) error {
defer exp.Conn.SetReadDeadline(time.Time{})
exp.Conn.SetReadDeadline(time.Now().Add(timeout))
Expand Down Expand Up @@ -75,18 +83,25 @@ func (b *BRecv) Invoke(ctx context.Context, exp *Expect, timeout time.Duration,
return nil
}

// BWipeBuf flushes any cached inbound data. You probably don't want to call this.
type BWipeBuf struct{}

// Invoke fulfils the Batcher interface
func (b *BWipeBuf) Invoke(ctx context.Context, exp *Expect, timeout time.Duration, batchIdx int) error {
exp.buf = nil
return nil
}

// BSend sends data through the net.Conn interface
type BSend struct {
Data string
// Data stores the string to be sent over the connection
Data string
// OnSuccess is optional, returning an error from here will
// stop the Batch operation and return with an error
OnSuccess func() error
}

// Invoke fulfils the Batcher interface
func (b *BSend) Invoke(ctx context.Context, exp *Expect, timeout time.Duration, batchIdx int) error {
select {
case <-ctx.Done():
Expand All @@ -109,43 +124,49 @@ func (b *BSend) Invoke(ctx context.Context, exp *Expect, timeout time.Duration,
return nil
}

// BSendDyn is like BSend however it determines what will be sent by calling the Data() callback
type BSendDyn struct {
Data func() string
OnSuccess func() error
Data func() string // Data is required, the result is treated like BSend.Data
OnSuccess func() error // OnSuccess is like BSend.OnSuccess, it is optional
}

// Invoke fulfils the Batcher interface
func (b *BSendDyn) Invoke(ctx context.Context, exp *Expect, timeout time.Duration, batchIdx int) error {
bs := BSend{Data: b.Data(), OnSuccess: b.OnSuccess}
return bs.Invoke(ctx, exp, timeout, batchIdx)
}

// BCallback calls the callback, nothing more. Useful for regression testing.
type BCallback struct {
Callback func()
}

// Invoke fulfils the Batcher interface
func (b *BCallback) Invoke(ctx context.Context, exp *Expect, timeout time.Duration, batchIdx int) error {
b.Callback()
return nil
}

type Encoding string
// type Encoding string

const (
EncUtf8 Encoding = ""
EncNone Encoding = "none"
)
// const (
// EncUtf8 Encoding = ""
// EncNone Encoding = "none"
// )

type Expect struct {
Encoding Encoding
Logger func(msg Log)
Conn net.Conn
buf []byte
//Encoding Encoding
Logger func(msg Log)
Conn net.Conn
buf []byte
}

// Batch calls Batcher with a context.Background() for context
func (e *Expect) Batch(timeout time.Duration, batches ...Batcher) error {
return e.BatchContext(context.Background(), timeout, batches...)
}

// Error is the struct that all errors returned by Batch/BatchContext are wrapped in
type Error struct {
BatchIdx int
Orig error
Expand All @@ -156,6 +177,9 @@ func (err *Error) Error() string {
return fmt.Sprintf("Batch op with type %T at index %d failed: %s", err.Op, err.BatchIdx, err.Orig)
}

// BatchContext allows multiple batched requests/responses. timeout operates on a
// per command basis, to limit the total time that can be used set a deadline on
// the passed in context.
func (e *Expect) BatchContext(ctx context.Context, timeout time.Duration, ops ...Batcher) error {
done := make(chan struct{})
defer close(done)
Expand Down

0 comments on commit 1701082

Please sign in to comment.