<commit_before>package ordering

import (
	"context"
	"sync"
	"testing"
	"time"

	"github.com/relab/gorums"
	"github.com/relab/gorums/internal/leakcheck"
	"google.golang.org/grpc"
)

type testSrv struct {
	sync.Mutex
	lastNum uint64
}

func (s *testSrv) isInOrder(num uint64) bool {
	s.Lock()
	defer s.Unlock()
	if num > s.lastNum {
		s.lastNum = num
		return true
	}
	return false
}

func (s *testSrv) QC(_ context.Context, req *Request, out func(*Response, error)) {
	out(&Response{
		InOrder: s.isInOrder(req.GetNum()),
	}, nil)
}

func (s *testSrv) QCFuture(_ context.Context, req *Request, out func(*Response, error)) {
	out(&Response{
		InOrder: s.isInOrder(req.GetNum()),
	}, nil)
}

func (s *testSrv) UnaryRPC(_ context.Context, req *Request, out func(*Response, error)) {
	out(&Response{
		InOrder: s.isInOrder(req.GetNum()),
	}, nil)
}

type testQSpec struct {
	quorum int
}

func (q testQSpec) qf(replies map[uint32]*Response) (*Response, bool) {
	if len(replies) < q.quorum {
		return nil, false
	}
	for _, reply := range replies {
		if !reply.InOrder {
			return reply, true
		}
	}
	var reply *Response
	for _, r := range replies {
		reply = r
		break
	}
	return reply, true
}

func (q testQSpec) QCQF(_ *Request, replies map[uint32]*Response) (*Response, bool) {
	return q.qf(replies)
}

func (q testQSpec) QCFutureQF(_ *Request, replies map[uint32]*Response) (*Response, bool) {
	return q.qf(replies)
}

func (q testQSpec) AsyncHandlerQF(_ *Request, replies map[uint32]*Response) (*Response, bool) {
	return q.qf(replies)
}

func setup(t *testing.T, cfgSize int) (cfg *Configuration, teardown func()) {
	t.Helper()
	addrs, closeServers := gorums.TestSetup(t, cfgSize, func() interface{} {
		srv := NewServer()
		RegisterGorumsTestServer(srv, &testSrv{})
		return srv
	})
	mgr, err := NewManager(gorums.WithNodeList(addrs), gorums.WithDialTimeout(100*time.Millisecond), gorums.WithGrpcDialOptions(
		grpc.WithBlock(), grpc.WithInsecure(),
	))
	if err != nil {
		t.Fatalf("Failed to start manager: %v", err)
	}
	cfg, err = mgr.NewConfiguration(mgr.NodeIDs(), &testQSpec{cfgSize})
	if err != nil {
		t.Fatalf("Failed to create configuration: %v", err)
	}
	teardown = func() {
		mgr.Close()
		closeServers()
	}
	return cfg, teardown
}

func TestUnaryRPCOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 1)
	defer teardown()
	node := cfg.Nodes()[0]
	// begin test
	numRuns := 1 << 16
	for i := 1; i < numRuns; i++ {
		resp, err := node.UnaryRPC(context.Background(), &Request{Num: uint64(i)})
		if err != nil {
			t.Fatalf("RPC error: %v", err)
		}
		if !resp.GetInOrder() {
			t.Fatalf("Message received out of order.")
		}
	}
}

func TestQCOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 4)
	defer teardown()
	// begin test
	numRuns := 1 << 16
	for i := 1; i < numRuns; i++ {
		resp, err := cfg.QC(context.Background(), &Request{Num: uint64(i)})
		if err != nil {
			t.Fatalf("QC error: %v", err)
		}
		if !resp.GetInOrder() {
			t.Fatalf("Message received out of order.")
		}
	}
}

func TestQCFutureOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 4)
	defer teardown()
	ctx, cancel := context.WithCancel(context.Background())
	// begin test
	var wg sync.WaitGroup
	numRuns := 1 << 16
	for i := 1; i < numRuns; i++ {
		promise := cfg.QCFuture(ctx, &Request{Num: uint64(i)})
		wg.Add(1)
		go func(promise *FutureResponse) {
			defer wg.Done()
			resp, err := promise.Get()
			if err != nil {
				if qcError, ok := err.(gorums.QuorumCallError); ok {
					if qcError.Reason == context.Canceled.Error() {
						return
					}
				}
				t.Errorf("QC error: %v", err)
			}
			if !resp.GetInOrder() && !t.Failed() {
				t.Errorf("Message received out of order.")
			}
		}(promise)
	}
	wg.Wait()
	cancel()
}

func TestMixedOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 4)
	defer teardown()
	nodes := cfg.Nodes()
	// begin test
	numRuns := 1 << 15
	num := uint64(1)
	for i := 0; i < numRuns; i++ {
		resp, err := cfg.QC(context.Background(), &Request{Num: num})
		num++
		if err != nil {
			t.Fatalf("QC error: %v", err)
		}
		if !resp.GetInOrder() {
			t.Fatalf("Message received out of order.")
		}
		var wg sync.WaitGroup
		wg.Add(len(nodes))
		for _, node := range nodes {
			go func(node *Node) {
				defer wg.Done()
				resp, err := node.UnaryRPC(context.Background(), &Request{Num: num})
				if err != nil {
					t.Errorf("RPC error: %v", err)
					return
				}
				if !resp.GetInOrder() {
					t.Errorf("Message received out of order.")
					return
				}
			}(node)
		}
		wg.Wait()
		num++
	}
}
<commit_msg>order_test: limit each test to 5 seconds<commit_after>package ordering

import (
	"context"
	"sync"
	"testing"
	"time"

	"github.com/relab/gorums"
	"github.com/relab/gorums/internal/leakcheck"
	"google.golang.org/grpc"
)

type testSrv struct {
	sync.Mutex
	lastNum uint64
}

func (s *testSrv) isInOrder(num uint64) bool {
	s.Lock()
	defer s.Unlock()
	if num > s.lastNum {
		s.lastNum = num
		return true
	}
	return false
}

func (s *testSrv) QC(_ context.Context, req *Request, out func(*Response, error)) {
	out(&Response{
		InOrder: s.isInOrder(req.GetNum()),
	}, nil)
}

func (s *testSrv) QCFuture(_ context.Context, req *Request, out func(*Response, error)) {
	out(&Response{
		InOrder: s.isInOrder(req.GetNum()),
	}, nil)
}

func (s *testSrv) UnaryRPC(_ context.Context, req *Request, out func(*Response, error)) {
	out(&Response{
		InOrder: s.isInOrder(req.GetNum()),
	}, nil)
}

type testQSpec struct {
	quorum int
}

func (q testQSpec) qf(replies map[uint32]*Response) (*Response, bool) {
	if len(replies) < q.quorum {
		return nil, false
	}
	for _, reply := range replies {
		if !reply.InOrder {
			return reply, true
		}
	}
	var reply *Response
	for _, r := range replies {
		reply = r
		break
	}
	return reply, true
}

func (q testQSpec) QCQF(_ *Request, replies map[uint32]*Response) (*Response, bool) {
	return q.qf(replies)
}

func (q testQSpec) QCFutureQF(_ *Request, replies map[uint32]*Response) (*Response, bool) {
	return q.qf(replies)
}

func (q testQSpec) AsyncHandlerQF(_ *Request, replies map[uint32]*Response) (*Response, bool) {
	return q.qf(replies)
}

func setup(t *testing.T, cfgSize int) (cfg *Configuration, teardown func()) {
	t.Helper()
	addrs, closeServers := gorums.TestSetup(t, cfgSize, func() interface{} {
		srv := NewServer()
		RegisterGorumsTestServer(srv, &testSrv{})
		return srv
	})
	mgr, err := NewManager(gorums.WithNodeList(addrs), gorums.WithDialTimeout(100*time.Millisecond), gorums.WithGrpcDialOptions(
		grpc.WithBlock(), grpc.WithInsecure(),
	))
	if err != nil {
		t.Fatalf("Failed to start manager: %v", err)
	}
	cfg, err = mgr.NewConfiguration(mgr.NodeIDs(), &testQSpec{cfgSize})
	if err != nil {
		t.Fatalf("Failed to create configuration: %v", err)
	}
	teardown = func() {
		mgr.Close()
		closeServers()
	}
	return cfg, teardown
}

func TestUnaryRPCOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 1)
	defer teardown()
	node := cfg.Nodes()[0]
	// begin test
	stopTime := time.Now().Add(5 * time.Second)
	i := 0
	for time.Now().Before(stopTime) {
		i++
		resp, err := node.UnaryRPC(context.Background(), &Request{Num: uint64(i)})
		if err != nil {
			t.Fatalf("RPC error: %v", err)
		}
		if !resp.GetInOrder() {
			t.Fatalf("Message received out of order.")
		}
	}
}

func TestQCOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 4)
	defer teardown()
	// begin test
	stopTime := time.Now().Add(5 * time.Second)
	i := 0
	for time.Now().Before(stopTime) {
		i++
		resp, err := cfg.QC(context.Background(), &Request{Num: uint64(i)})
		if err != nil {
			t.Fatalf("QC error: %v", err)
		}
		if !resp.GetInOrder() {
			t.Fatalf("Message received out of order.")
		}
	}
}

func TestQCFutureOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 4)
	defer teardown()
	ctx, cancel := context.WithCancel(context.Background())
	// begin test
	var wg sync.WaitGroup
	stopTime := time.Now().Add(5 * time.Second)
	i := 0
	for time.Now().Before(stopTime) {
		i++
		promise := cfg.QCFuture(ctx, &Request{Num: uint64(i)})
		wg.Add(1)
		go func(promise *FutureResponse) {
			defer wg.Done()
			resp, err := promise.Get()
			if err != nil {
				if qcError, ok := err.(gorums.QuorumCallError); ok {
					if qcError.Reason == context.Canceled.Error() {
						return
					}
				}
				t.Errorf("QC error: %v", err)
			}
			if !resp.GetInOrder() && !t.Failed() {
				t.Errorf("Message received out of order.")
			}
		}(promise)
	}
	wg.Wait()
	cancel()
}

func TestMixedOrdering(t *testing.T) {
	defer leakcheck.Check(t)
	cfg, teardown := setup(t, 4)
	defer teardown()
	nodes := cfg.Nodes()
	// begin test
	stopTime := time.Now().Add(5 * time.Second)
	i := 0
	for time.Now().Before(stopTime) {
		resp, err := cfg.QC(context.Background(), &Request{Num: uint64(i)})
		i++
		if err != nil {
			t.Fatalf("QC error: %v", err)
		}
		if !resp.GetInOrder() {
			t.Fatalf("Message received out of order.")
		}
		var wg sync.WaitGroup
		wg.Add(len(nodes))
		for _, node := range nodes {
			go func(node *Node) {
				defer wg.Done()
				resp, err := node.UnaryRPC(context.Background(), &Request{Num: uint64(i)})
				if err != nil {
					t.Errorf("RPC error: %v", err)
					return
				}
				if !resp.GetInOrder() {
					t.Errorf("Message received out of order.")
					return
				}
			}(node)
		}
		wg.Wait()
		i++
	}
}
<|endoftext|>


<commit_before>// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

// Example service program that beeps. It demonstrates how to
// create a service and install / remove it on a computer.
// It also shows how to stop / start / pause / continue any service,
// and how to write to event log. It also shows how to use debug
// facilities available in debug package.
//
package main

import (
	"code.google.com/p/winsvc/svc"
	"fmt"
	"log"
	"os"
	"strings"
)

func usage(errmsg string) {
	fmt.Fprintf(os.Stderr,
		"%s\n\n"+
			"usage: %s <command>\n"+
			"       where <command> is one of\n"+
			"       install, remove, debug, start, stop, pause or continue.\n",
		errmsg, os.Args[0])
	os.Exit(2)
}

func main() {
	const svcName = "myservice"

	isIntSess, err := svc.IsAnInteractiveSession()
	if err != nil {
		log.Fatalf("failed to determine if we are running in an interactive session: %v", err)
	}
	if !isIntSess {
		runService(svcName, false)
		return
	}

	if len(os.Args) < 2 {
		usage("no command specified")
	}

	cmd := strings.ToLower(os.Args[1])
	switch cmd {
	case "debug":
		runService(svcName, true)
		return
	case "install":
		err = installService(svcName, "my service")
	case "remove":
		err = removeService(svcName)
	case "start":
		err = startService(svcName)
	case "stop":
		err = controlService(svcName, svc.Stop, svc.Stopped)
	case "pause":
		err = controlService(svcName, svc.Pause, svc.Paused)
	case "continue":
		err = controlService(svcName, svc.Continue, svc.Running)
	default:
		usage(fmt.Sprintf("invalid command %s", cmd))
	}
	if err != nil {
		log.Fatalf("failed to %s %s: %v", cmd, svcName, err)
	}
	return
}
<commit_msg>initial<commit_after>// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

// Example service program that beeps. It demonstrates how to
// create a service and install / remove it on a computer.
// It also shows how to stop / start / pause / continue any service,
// and how to write to event log. It also shows how to use debug
// facilities available in debug package.
//
package main

import (
	"code.google.com/p/winsvc/svc"
	"fmt"
	"log"
	"os"
	"strings"
)

func usage(errmsg string) {
	fmt.Fprintf(os.Stderr,
		"%s\n\n"+
			"usage: %s <command>\n"+
			"       where <command> is one of\n"+
			"       install, remove, debug, start, stop, pause or continue.\n",
		errmsg, os.Args[0])
	os.Exit(2)
}

func main() {
	const svcName = "HUUZKEE Node Service"

	isIntSess, err := svc.IsAnInteractiveSession()
	if err != nil {
		log.Fatalf("failed to determine if we are running in an interactive session: %v", err)
	}
	if !isIntSess {
		runService(svcName, false)
		return
	}

	if len(os.Args) < 2 {
		usage("no command specified")
	}

	cmd := strings.ToLower(os.Args[1])
	switch cmd {
	case "debug":
		runService(svcName, true)
		return
	case "install":
		err = installService(svcName, "my service")
	case "remove":
		err = removeService(svcName)
	case "start":
		err = startService(svcName)
	case "stop":
		err = controlService(svcName, svc.Stop, svc.Stopped)
	case "pause":
		err = controlService(svcName, svc.Pause, svc.Paused)
	case "continue":
		err = controlService(svcName, svc.Continue, svc.Running)
	default:
		usage(fmt.Sprintf("invalid command %s", cmd))
	}
	if err != nil {
		log.Fatalf("failed to %s %s: %v", cmd, svcName, err)
	}
	return
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"log"
	"net/http"

	"github.com/jamal/stom"
	"github.com/jamal/stom/middleware"
)

var db *sql.DB

type Context struct {
	DB   *sql.DB
	User *User
}

// User is used as an example of something you could store in Context
type User struct {
	ID   int
	Name string
}

func NewContext(r *http.Request) *Context {
	return &Context{db, &User{1, "Jamal"}}
}

func Index(w http.ResponseWriter, r *http.Request) {
	stom.WriteString(w, "Welcome")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	ctx := NewContext(r)
	stom.WriteString(w, "Hello, %s", ctx.User.Name)
}

func HelloName(w http.ResponseWriter, r *http.Request) {
	stom.WriteString(w, "Hello, %s", r.FormValue("name"))
}

func main() {
	s := stom.New()
	s.UseAfter(middleware.Logger{})

	s.Get("/", Index)
	s.Get("/hello", Hello)
	s.Get("/hello/:name", HelloName)

	log.Fatal(http.ListenAndServe(":8080", s))
}
<commit_msg>Added a small example of a json response<commit_after>package main

import (
	"database/sql"
	"log"
	"net/http"

	"github.com/jamal/stom"
	"github.com/jamal/stom/middleware"
)

var db *sql.DB

type Context struct {
	DB   *sql.DB
	User *User
}

// User is used as an example of something you could store in Context
type User struct {
	ID   int
	Name string
}

func NewContext(r *http.Request) *Context {
	return &Context{db, &User{1, "Jamal"}}
}

func Index(w http.ResponseWriter, r *http.Request) {
	stom.WriteString(w, "Welcome")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	ctx := NewContext(r)
	stom.WriteString(w, "Hello, %s", ctx.User.Name)
}

func HelloName(w http.ResponseWriter, r *http.Request) {
	stom.WriteString(w, "Hello, %s", r.FormValue("name"))
}

func HelloJSON(w http.ResponseWriter, r *http.Request) {
	stom.WriteJSON(w, map[string]string{"message": "Hello!"})
}

func main() {
	s := stom.New()
	s.UseAfter(middleware.Logger{})

	s.Get("/", Index)
	s.Get("/hello", Hello)
	s.Get("/hello/:name", HelloName)
	s.Get("/hello.json", HelloJSON)

	log.Fatal(http.ListenAndServe(":8080", s))
}
<|endoftext|>


<commit_before>package main

import (
	"crypto/md5"
	"crypto/tls"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"strings"
	"sync"

	_ "net/http/pprof"

	"github.com/lucas-clemente/quic-go/h2quic"
	"github.com/lucas-clemente/quic-go/testdata"
	"github.com/lucas-clemente/quic-go/utils"
)

type binds []string

func (b binds) String() string {
	return strings.Join(b, ",")
}

func (b *binds) Set(v string) error {
	*b = strings.Split(v, ",")
	return nil
}

// Size is needed by the /demo/upload handler to determine the size of the uploaded file
type Size interface {
	Size() int64
}

func init() {
	http.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) {
		// Small 40x40 png
		w.Write([]byte{
			0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
			0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28,
			0x01, 0x03, 0x00, 0x00, 0x00, 0xb6, 0x30, 0x2a, 0x2e, 0x00, 0x00, 0x00,
			0x03, 0x50, 0x4c, 0x54, 0x45, 0x5a, 0xc3, 0x5a, 0xad, 0x38, 0xaa, 0xdb,
			0x00, 0x00, 0x00, 0x0b, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x63, 0x18,
			0x61, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x01, 0xe2, 0xb8, 0x75, 0x22, 0x00,
			0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
		})
	})

	http.HandleFunc("/demo/tiles", func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "")
		for i := 0; i < 200; i++ {
			fmt.Fprintf(w, `<img src="/demo/tile?cachebust=%d">`, i)
		}
		io.WriteString(w, "")
	})

	http.HandleFunc("/demo/echo", func(w http.ResponseWriter, r *http.Request) {
		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			fmt.Printf("error reading body while handling /echo: %s\n", err.Error())
		}
		w.Write(body)
	})

	// accept file uploads and return the MD5 of the uploaded file
	// maximum accepted file size is 1 GB
	http.HandleFunc("/demo/upload", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPost {
			var err error
			err = r.ParseMultipartForm(1 << 30) // 1 GB
			if err == nil {
				var file multipart.File
				file, _, err = r.FormFile("uploadfile")
				if err == nil {
					var size int64
					if sizeInterface, ok := file.(Size); ok {
						size = sizeInterface.Size()
						b := make([]byte, size)
						file.Read(b)
						md5 := md5.Sum(b)
						fmt.Fprintf(w, "%x", md5)
						return
					}
					err = errors.New("Couldn't get uploaded file size.")
				}
			}
			if err != nil {
				utils.Infof("Error receiving upload: %#v", err)
			}
		}
		io.WriteString(w, `<form action="/demo/upload" method="post" enctype="multipart/form-data">
				<input type="file" name="uploadfile">

				<input type="submit">
			</form>`)
	})
}

func main() {
	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
	// runtime.SetBlockProfileRate(1)

	verbose := flag.Bool("v", false, "verbose")
	bs := binds{}
	flag.Var(&bs, "bind", "bind to")
	certPath := flag.String("certpath", "", "certificate directory")
	www := flag.String("www", "/var/www", "www data")
	flag.Parse()

	if *verbose {
		utils.SetLogLevel(utils.LogLevelDebug)
	} else {
		utils.SetLogLevel(utils.LogLevelInfo)
	}

	var tlsConfig *tls.Config
	if *certPath == "" {
		tlsConfig = testdata.GetTLSConfig()
	} else {
		var err error
		cert, err := tls.LoadX509KeyPair(*certPath+"/fullchain.pem", *certPath+"/privkey.pem")
		if err != nil {
			panic(err)
		}
		tlsConfig = &tls.Config{
			Certificates: []tls.Certificate{cert},
		}
	}

	http.Handle("/", http.FileServer(http.Dir(*www)))

	if len(bs) == 0 {
		bs = binds{"localhost:6121"}
	}

	var wg sync.WaitGroup
	wg.Add(len(bs))
	for _, b := range bs {
		bCap := b
		go func() {
			server := h2quic.Server{
				// CloseAfterFirstRequest: true,
				Server: &http.Server{
					Addr:      bCap,
					TLSConfig: tlsConfig,
				},
			}
			err := server.ListenAndServe()
			if err != nil {
				fmt.Println(err)
			}
			wg.Done()
		}()
	}
	wg.Wait()
}
<commit_msg>add a -tcp flag to the example server that activates parallel TCP<commit_after>package main

import (
	"crypto/md5"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"os"
	"strings"
	"sync"

	_ "net/http/pprof"

	"github.com/lucas-clemente/quic-go/h2quic"
	"github.com/lucas-clemente/quic-go/utils"
)

type binds []string

func (b binds) String() string {
	return strings.Join(b, ",")
}

func (b *binds) Set(v string) error {
	*b = strings.Split(v, ",")
	return nil
}

// Size is needed by the /demo/upload handler to determine the size of the uploaded file
type Size interface {
	Size() int64
}

func init() {
	http.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) {
		// Small 40x40 png
		w.Write([]byte{
			0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
			0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28,
			0x01, 0x03, 0x00, 0x00, 0x00, 0xb6, 0x30, 0x2a, 0x2e, 0x00, 0x00, 0x00,
			0x03, 0x50, 0x4c, 0x54, 0x45, 0x5a, 0xc3, 0x5a, 0xad, 0x38, 0xaa, 0xdb,
			0x00, 0x00, 0x00, 0x0b, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x63, 0x18,
			0x61, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x01, 0xe2, 0xb8, 0x75, 0x22, 0x00,
			0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
		})
	})

	http.HandleFunc("/demo/tiles", func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "")
		for i := 0; i < 200; i++ {
			fmt.Fprintf(w, `<img src="/demo/tile?cachebust=%d">`, i)
		}
		io.WriteString(w, "")
	})

	http.HandleFunc("/demo/echo", func(w http.ResponseWriter, r *http.Request) {
		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			fmt.Printf("error reading body while handling /echo: %s\n", err.Error())
		}
		w.Write(body)
	})

	// accept file uploads and return the MD5 of the uploaded file
	// maximum accepted file size is 1 GB
	http.HandleFunc("/demo/upload", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodPost {
			var err error
			err = r.ParseMultipartForm(1 << 30) // 1 GB
			if err == nil {
				var file multipart.File
				file, _, err = r.FormFile("uploadfile")
				if err == nil {
					var size int64
					if sizeInterface, ok := file.(Size); ok {
						size = sizeInterface.Size()
						b := make([]byte, size)
						file.Read(b)
						md5 := md5.Sum(b)
						fmt.Fprintf(w, "%x", md5)
						return
					}
					err = errors.New("Couldn't get uploaded file size.")
				}
			}
			if err != nil {
				utils.Infof("Error receiving upload: %#v", err)
			}
		}
		io.WriteString(w, `<form action="/demo/upload" method="post" enctype="multipart/form-data">
				<input type="file" name="uploadfile">

				<input type="submit">
			</form>`)
	})
}

func main() {
	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
	// runtime.SetBlockProfileRate(1)

	verbose := flag.Bool("v", false, "verbose")
	bs := binds{}
	flag.Var(&bs, "bind", "bind to")
	certPath := flag.String("certpath", os.Getenv("GOPATH")+"/src/github.com/lucas-clemente/quic-go/example/", "certificate directory")
	www := flag.String("www", "/var/www", "www data")
	tcp := flag.Bool("tcp", false, "also listen on TCP")
	flag.Parse()

	if *verbose {
		utils.SetLogLevel(utils.LogLevelDebug)
	} else {
		utils.SetLogLevel(utils.LogLevelInfo)
	}

	certFile := *certPath + "/fullchain.pem"
	keyFile := *certPath + "/privkey.pem"

	http.Handle("/", http.FileServer(http.Dir(*www)))

	if len(bs) == 0 {
		bs = binds{"localhost:6121"}
	}

	var wg sync.WaitGroup
	wg.Add(len(bs))
	for _, b := range bs {
		bCap := b
		go func() {
			var err error
			if *tcp {
				err = h2quic.ListenAndServe(bCap, certFile, keyFile, nil)
			} else {
				err = h2quic.ListenAndServeQUIC(bCap, certFile, keyFile, nil)
			}
			if err != nil {
				fmt.Println(err)
			}
			wg.Done()
		}()
	}
	wg.Wait()
}
<|endoftext|>


<commit_before>package chromedp_test

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"

	"github.com/chromedp/chromedp"
)

func writeHTML(content string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, strings.TrimSpace(content))
	})
}

func ExampleTitle() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	ts := httptest.NewServer(writeHTML(`

	<title>fancy website title</title>

	`))
	defer ts.Close()

	var title string
	if err := chromedp.Run(ctx,
		chromedp.Navigate(ts.URL),
		chromedp.WaitVisible("#loaded", chromedp.ByID),
		chromedp.Title(&title),
	); err != nil {
		panic(err)
	}
	fmt.Println(title)

	// Output:
	// fancy website title
}

func ExampleExecAllocator() {
	dir, err := ioutil.TempDir("", "chromedp-example")
	if err != nil {
		panic(err)
	}
	defer os.RemoveAll(dir)

	opts := []chromedp.ExecAllocatorOption{
		chromedp.NoFirstRun,
		chromedp.NoDefaultBrowserCheck,
		chromedp.Headless,
		chromedp.DisableGPU,
		chromedp.UserDataDir(dir),
	}

	allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	defer cancel()

	// also set up a custom logger
	taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
	defer cancel()

	// ensure that the browser process is started
	if err := chromedp.Run(taskCtx); err != nil {
		panic(err)
	}

	path := filepath.Join(dir, "DevToolsActivePort")
	bs, err := ioutil.ReadFile(path)
	if err != nil {
		panic(err)
	}
	lines := bytes.Split(bs, []byte("\n"))
	fmt.Printf("DevToolsActivePort has %d lines\n", len(lines))

	// Output:
	// DevToolsActivePort has 2 lines
}

func ExampleNewContext_manyTabs() {
	// new browser, first tab
	ctx1, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	// ensure the first tab is created
	if err := chromedp.Run(ctx1); err != nil {
		panic(err)
	}

	// same browser, second tab
	ctx2, _ := chromedp.NewContext(ctx1)

	// ensure the second tab is created
	if err := chromedp.Run(ctx2); err != nil {
		panic(err)
	}

	c1 := chromedp.FromContext(ctx1)
	c2 := chromedp.FromContext(ctx2)

	fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)
	fmt.Printf("Same tab: %t\n", c1.Target == c2.Target)

	// Output:
	// Same browser: true
	// Same tab: false
}
<commit_msg>add an example to listen for console log events<commit_after>package chromedp_test

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"

	cdplog "github.com/chromedp/cdproto/log"
	cdpruntime "github.com/chromedp/cdproto/runtime"
	"github.com/chromedp/chromedp"
)

func writeHTML(content string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, strings.TrimSpace(content))
	})
}

func ExampleTitle() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	ts := httptest.NewServer(writeHTML(`

	<title>fancy website title</title>

	`))
	defer ts.Close()

	var title string
	if err := chromedp.Run(ctx,
		chromedp.Navigate(ts.URL),
		chromedp.WaitVisible("#loaded", chromedp.ByID),
		chromedp.Title(&title),
	); err != nil {
		panic(err)
	}
	fmt.Println(title)

	// Output:
	// fancy website title
}

func ExampleExecAllocator() {
	dir, err := ioutil.TempDir("", "chromedp-example")
	if err != nil {
		panic(err)
	}
	defer os.RemoveAll(dir)

	opts := []chromedp.ExecAllocatorOption{
		chromedp.NoFirstRun,
		chromedp.NoDefaultBrowserCheck,
		chromedp.Headless,
		chromedp.DisableGPU,
		chromedp.UserDataDir(dir),
	}

	allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	defer cancel()

	// also set up a custom logger
	taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
	defer cancel()

	// ensure that the browser process is started
	if err := chromedp.Run(taskCtx); err != nil {
		panic(err)
	}

	path := filepath.Join(dir, "DevToolsActivePort")
	bs, err := ioutil.ReadFile(path)
	if err != nil {
		panic(err)
	}
	lines := bytes.Split(bs, []byte("\n"))
	fmt.Printf("DevToolsActivePort has %d lines\n", len(lines))

	// Output:
	// DevToolsActivePort has 2 lines
}

func ExampleNewContext_manyTabs() {
	// new browser, first tab
	ctx1, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	// ensure the first tab is created
	if err := chromedp.Run(ctx1); err != nil {
		panic(err)
	}

	// same browser, second tab
	ctx2, _ := chromedp.NewContext(ctx1)

	// ensure the second tab is created
	if err := chromedp.Run(ctx2); err != nil {
		panic(err)
	}

	c1 := chromedp.FromContext(ctx1)
	c2 := chromedp.FromContext(ctx2)

	fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)
	fmt.Printf("Same tab: %t\n", c1.Target == c2.Target)

	// Output:
	// Same browser: true
	// Same tab: false
}

func ExampleListenTarget_consoleLog() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	ts := httptest.NewServer(writeHTML(`

	`))
	defer ts.Close()

	chromedp.ListenTarget(ctx, func(ev interface{}) {
		switch ev := ev.(type) {
		case *cdpruntime.EventConsoleAPICalled:
			fmt.Printf("console.%s call:\n", ev.Type)
			for _, arg := range ev.Args {
				fmt.Printf("%s - %s\n", arg.Type, arg.Value)
			}
		}
	})

	if err := chromedp.Run(ctx,
		cdplog.Enable(),
		chromedp.Navigate(ts.URL),
		chromedp.WaitVisible("#loaded", chromedp.ByID),
	); err != nil {
		panic(err)
	}

	// Output:
	// console.log call:
	// string - "hello js world"
}
<|endoftext|>


<commit_before>// Copyright 2015 The syscallinfo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syscallinfo_test

import (
	"fmt"

	"github.com/jroimartin/syscallinfo"
	"github.com/jroimartin/syscallinfo/linux_386"
)

func ExampleRepr() {
	r := syscallinfo.NewResolver(linux_386.SyscallTable)
	sc, err := r.Syscall(3)
	if err != nil {
		return
	}
	str, err := sc.Repr(4, 1, 2, 3)
	if err != nil {
		return
	}
	fmt.Println(str)

	// Output:
	// read(1, 0x00000002, 0x00000003) = 0x00000004
}
<commit_msg>Modify example name to be included by godoc<commit_after>// Copyright 2015 The syscallinfo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package syscallinfo_test

import (
	"fmt"

	"github.com/jroimartin/syscallinfo"
	"github.com/jroimartin/syscallinfo/linux_386"
)

func ExampleSyscall_Repr() {
	r := syscallinfo.NewResolver(linux_386.SyscallTable)
	sc, err := r.Syscall(3)
	if err != nil {
		return
	}
	str, err := sc.Repr(4, 1, 2, 3)
	if err != nil {
		return
	}
	fmt.Println(str)

	// Output:
	// read(1, 0x00000002, 0x00000003) = 0x00000004
}
<|endoftext|>


<commit_before>package msgqueue_test

import (
	"errors"
	"fmt"
	"math"
	"time"

	"github.com/go-msgqueue/msgqueue"
	"github.com/go-msgqueue/msgqueue/memqueue"

	"github.com/go-redis/redis"
	timerate "golang.org/x/time/rate"
)

func redisRing() *redis.Ring {
	ring := redis.NewRing(&redis.RingOptions{
		Addrs:    map[string]string{"0": ":6379"},
		PoolSize: 100,
	})
	err := ring.FlushDb().Err()
	if err != nil {
		panic(err)
	}
	return ring
}

func timeSince(start time.Time) time.Duration {
	secs := float64(time.Since(start)) / float64(time.Second)
	return time.Duration(math.Floor(secs)) * time.Second
}

func Example_retryOnError() {
	start := time.Now()
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler: func() error {
			fmt.Println("retried in", timeSince(start))
			return errors.New("fake error")
		},
		RetryLimit: 3,
		MinBackoff: time.Second,
	})
	defer q.Close()
	q.Processor().Stop()

	q.Call()
	q.Processor().ProcessAll()
	// Output: retried in 0s
	// retried in 1s
	// retried in 3s
}

func Example_messageDelay() {
	start := time.Now()
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler: func() {
			fmt.Println("processed with delay", timeSince(start))
		},
	})
	defer q.Close()
	q.Processor().Stop()

	msg := msgqueue.NewMessage()
	msg.Delay = time.Second
	q.Add(msg)
	q.Processor().ProcessAll()
	// Output: processed with delay 1s
}

func Example_rateLimit() {
	start := time.Now()
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler:   func() {},
		Redis:     redisRing(),
		RateLimit: timerate.Every(time.Second),
	})

	for i := 0; i < 3; i++ {
		q.Call()
	}

	// Close queue to make sure all messages are processed.
	_ = q.Close()

	fmt.Println("3 messages processed in", timeSince(start))
	// Output: 3 messages processed in 2s
}
<commit_msg>Add CallOnce example<commit_after>package msgqueue_test

import (
	"errors"
	"fmt"
	"math"
	"time"

	"github.com/go-msgqueue/msgqueue"
	"github.com/go-msgqueue/msgqueue/memqueue"

	"github.com/go-redis/redis"
	timerate "golang.org/x/time/rate"
)

func redisRing() *redis.Ring {
	ring := redis.NewRing(&redis.RingOptions{
		Addrs:    map[string]string{"0": ":6379"},
		PoolSize: 100,
	})
	err := ring.FlushDb().Err()
	if err != nil {
		panic(err)
	}
	return ring
}

func timeSince(start time.Time) time.Duration {
	secs := float64(time.Since(start)) / float64(time.Second)
	return time.Duration(math.Floor(secs)) * time.Second
}

func Example_retryOnError() {
	start := time.Now()
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler: func() error {
			fmt.Println("retried in", timeSince(start))
			return errors.New("fake error")
		},
		RetryLimit: 3,
		MinBackoff: time.Second,
	})
	defer q.Close()
	q.Processor().Stop()

	q.Call()
	q.Processor().ProcessAll()
	// Output: retried in 0s
	// retried in 1s
	// retried in 3s
}

func Example_messageDelay() {
	start := time.Now()
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler: func() {
			fmt.Println("processed with delay", timeSince(start))
		},
	})
	defer q.Close()
	q.Processor().Stop()

	msg := msgqueue.NewMessage()
	msg.Delay = time.Second
	q.Add(msg)
	q.Processor().ProcessAll()
	// Output: processed with delay 1s
}

func Example_rateLimit() {
	start := time.Now()
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler:   func() {},
		Redis:     redisRing(),
		RateLimit: timerate.Every(time.Second),
	})

	for i := 0; i < 3; i++ {
		q.Call()
	}

	// Close queue to make sure all messages are processed.
	_ = q.Close()

	fmt.Println("3 messages processed in", timeSince(start))
	// Output: 3 messages processed in 2s
}

func Example_once() {
	q := memqueue.NewQueue(&msgqueue.Options{
		Handler: func(name string) {
			fmt.Println("hello", name)
		},
		Redis:     redisRing(),
		RateLimit: timerate.Every(time.Second),
	})

	for _, name := range []string{"world", "adele"} {
		for i := 0; i < 10; i++ {
			// Call once in a second.
			q.CallOnce(time.Second, name)
		}
	}

	// Close queue to make sure all messages are processed.
	_ = q.Close()

	// Output: hello world
	// hello adele
}
<|endoftext|>


<commit_before>package solve_test

import (
	"github.com/bertbaron/solve"
	"fmt"
)

type state struct {
	vector [5]byte
	cost   int
	index  int
}

func (s state) Id() interface{} {
	return s.vector
}

func (s state) Expand() []solve.State {
	n := len(s.vector) - 1
	steps := make([]solve.State, n, n)
	for i := 0; i < n; i++ {
		child := state{s.vector, s.cost + 1, i}
		child.vector[i], child.vector[i + 1] = child.vector[i + 1], child.vector[i]
		steps[i] = child
	}
	return steps
}

func (s state) IsGoal() bool {
	n := len(s.vector) - 1
	for i := 0; i < n; i++ {
		if s.vector[i] > s.vector[i + 1] {
			return false
		}
	}
	return true
}

func (s state) Cost() float64 {
	return float64(s.cost)
}

func (s state) Heuristic() float64 {
	return 0
}

// Finds the minumum number of swaps of neighbouring elements required to
// sort a vector
func Example() {
	s := state{[...]byte{3, 2, 5, 4, 1}, 0, -1}
	result := solve.NewSolver(s).
		Algorithm(solve.IDAstar).
		Constraint(solve.CHEAPEST_PATH).
		Solve()
	for _, st := range result.Solution {
		fmt.Printf("%v\n", st.(state).vector)
	}
	// Output:
	// [3 2 5 4 1]
	// [3 2 5 1 4]
	// [3 2 1 5 4]
	// [3 2 1 4 5]
	// [3 1 2 4 5]
	// [1 3 2 4 5]
	// [1 2 3 4 5]
}
<commit_msg>Added a simple example<commit_after>package solve_test

import (
	"github.com/bertbaron/solve"
	"fmt"
)

type state struct {
	vector [5]byte
	cost   int
	index  int
}

func (s state) Id() interface{} {
	return s.vector
}

func (s state) Expand() []solve.State {
	n := len(s.vector) - 1
	steps := make([]solve.State, n, n)
	for i := 0; i < n; i++ {
		child := state{s.vector, s.cost + 1, i}
		child.vector[i], child.vector[i + 1] = child.vector[i + 1], child.vector[i]
		steps[i] = child
	}
	return steps
}

func (s state) IsGoal() bool {
	n := len(s.vector) - 1
	for i := 0; i < n; i++ {
		if s.vector[i] > s.vector[i + 1] {
			return false
		}
	}
	return true
}

func (s state) Cost() float64 {
	return float64(s.cost)
}

func (s state) Heuristic() float64 {
	return 0
}

// Finds the minumum number of swaps of neighbouring elements required to
// sort a vector
func Example() {
	s := state{[...]byte{3, 2, 5, 4, 1}, 0, -1}
	result := solve.NewSolver(s).
		Algorithm(solve.IDAstar).
		Constraint(solve.NO_LOOP).
		Solve()
	for _, st := range result.Solution {
		fmt.Printf("%v\n", st.(state).vector)
	}
	// Output:
	// [3 2 5 4 1]
	// [3 2 5 1 4]
	// [3 2 1 5 4]
	// [3 2 1 4 5]
	// [3 1 2 4 5]
	// [1 3 2 4 5]
	// [1 2 3 4 5]
}
<|endoftext|>


<commit_before>package nats_test

import (
	"fmt"
	"github.com/apcera/nats"
	"time"
)

// Shows different ways to create a Conn
func ExampleConnect() {

	nats.Connect(nats.DefaultURL)
	nats.Connect("nats://derek:secretpassword@nats.apcera.com:421")

	opts := nats.Options{
		AllowReconnect : true,
		MaxReconnect   : 10,
		ReconnectWait  : 5 * time.Second,
		Timeout        : 1 * time.Second,
	}

	nc, _ := opts.Connect()
	nc.Close()
}

// This Example shows an asynchronous subscriber.
func ExampleConn_Subscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	nc.Subscribe("foo", func(m *nats.Msg) {
		fmt.Printf("Received a message: %s\n", string(m.Data))
	})
}

// This Example shows a synchronous subscriber.
func ExampleConn_SubscribeSync() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	m, err := sub.NextMsg(1 * time.Second)
	if err == nil {
		fmt.Printf("Received a message: %s\n", string(m.Data))
	} else {
		fmt.Println("NextMsg timed out.")
	}
}

func ExampleSubscription_NextMsg() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	m, err := sub.NextMsg(1*time.Second)
	if err == nil {
		fmt.Printf("Received a message: %s\n", string(m.Data))
	} else {
		fmt.Println("NextMsg timed out.")
	}
}

func ExampleSubscription_Unsubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	// ...
	sub.Unsubscribe()
}

func ExampleConn_Publish() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	nc.Publish("foo", []byte("Hello World!"))
}

func ExampleConn_PublishMsg() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
	nc.PublishMsg(msg)
}

func ExampleConn_Flush() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	msg := &nats.Msg{Subject:"foo", Reply:"bar", Data:[]byte("Hello World!")}
	for i := 0 ; i < 1000 ; i++ {
		nc.PublishMsg(msg)
	}
	err := nc.Flush()
	if err == nil {
		// Everything has been processed by the server for nc *Conn.
	}
}

func ExampleConn_FlushTimeout() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
	for i := 0; i < 1000; i++ {
		nc.PublishMsg(msg)
	}
	// Only wait for up to 1 second for Flush
	err := nc.FlushTimeout(1 * time.Second)
	if err == nil {
		// Everything has been processed by the server for nc *Conn.
	}
}

func ExampleConn_Request() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	nc.Subscribe("foo", func(m *nats.Msg) {
		nc.Publish(m.Reply, []byte("I will help you"))
	})
	nc.Request("foo", []byte("help"), 50*time.Millisecond)
}

func ExampleConn_QueueSubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	received := 0

	nc.QueueSubscribe("foo", "worker_group", func(_ *nats.Msg) {
		received += 1
	})
}

func ExampleSubscription_AutoUnsubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	received, wanted, total := 0, 10, 100

	sub, _ := nc.Subscribe("foo",  func(_ *nats.Msg) {
		received += 1
	})
	sub.AutoUnsubscribe(wanted)

	for i := 0; i < total; i++ {
		nc.Publish("foo", []byte("Hello"))
	}
	nc.Flush()

	fmt.Printf("Received = %d", received)
}

func ExampleConn_Close() {
	nc, _ := nats.Connect(nats.DefaultURL)
	nc.Close()
}

// Shows how to wrap a Conn into an EncodedConn
func ExampleNewEncodedConn() {
	nc, _ := nats.Connect(nats.DefaultURL)
	c, _ := nats.NewEncodedConn(nc, "json")
	c.Close()
}

// EncodedConn can publish virtually anything just
// by passing it in. The encoder will be used to properly
// encode the raw Go type
func ExampleEncodedConn_Publish() {
	nc, _ := nats.Connect(nats.DefaultURL)
	c, _ := nats.NewEncodedConn(nc, "json")
	defer c.Close()

	type person struct {
		Name     string
		Address  string
		Age      int
	}

	me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
	c.Publish("hello", me)
}

// EncodedConn's subscribers will automatically decode the
// wire data into the requested Go type using the Decode()
// method of the registered Encoder. The callback signature
// can also vary to include additional data, such as subject
// and reply subjects.
func ExampleEncodedConn_Subscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	c, _ := nats.NewEncodedConn(nc, "json")
	defer c.Close()

	type person struct {
		Name     string
		Address  string
		Age      int
	}

	c.Subscribe("hello", func(p *person) {
		fmt.Printf("Received a person! %+v\n", p)
	})

	c.Subscribe("hello", func(subj, reply string, p *person) {
		fmt.Printf("Received a person on subject %s! %+v\n", subj, p)
	})

	me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
	c.Publish("hello", me)
}
<commit_msg>gofmt<commit_after>package nats_test

import (
	"fmt"
	"github.com/apcera/nats"
	"time"
)

// Shows different ways to create a Conn
func ExampleConnect() {

	nats.Connect(nats.DefaultURL)
	nats.Connect("nats://derek:secretpassword@nats.apcera.com:421")

	opts := nats.Options{
		AllowReconnect: true,
		MaxReconnect:   10,
		ReconnectWait:  5 * time.Second,
		Timeout:        1 * time.Second,
	}

	nc, _ := opts.Connect()
	nc.Close()
}

// This Example shows an asynchronous subscriber.
func ExampleConn_Subscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	nc.Subscribe("foo", func(m *nats.Msg) {
		fmt.Printf("Received a message: %s\n", string(m.Data))
	})
}

// This Example shows a synchronous subscriber.
func ExampleConn_SubscribeSync() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	m, err := sub.NextMsg(1 * time.Second)
	if err == nil {
		fmt.Printf("Received a message: %s\n", string(m.Data))
	} else {
		fmt.Println("NextMsg timed out.")
	}
}

func ExampleSubscription_NextMsg() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	m, err := sub.NextMsg(1 * time.Second)
	if err == nil {
		fmt.Printf("Received a message: %s\n", string(m.Data))
	} else {
		fmt.Println("NextMsg timed out.")
	}
}

func ExampleSubscription_Unsubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	sub, _ := nc.SubscribeSync("foo")
	// ...
	sub.Unsubscribe()
}

func ExampleConn_Publish() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	nc.Publish("foo", []byte("Hello World!"))
}

func ExampleConn_PublishMsg() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
	nc.PublishMsg(msg)
}

func ExampleConn_Flush() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
	for i := 0; i < 1000; i++ {
		nc.PublishMsg(msg)
	}
	err := nc.Flush()
	if err == nil {
		// Everything has been processed by the server for nc *Conn.
	}
}

func ExampleConn_FlushTimeout() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
	for i := 0; i < 1000; i++ {
		nc.PublishMsg(msg)
	}
	// Only wait for up to 1 second for Flush
	err := nc.FlushTimeout(1 * time.Second)
	if err == nil {
		// Everything has been processed by the server for nc *Conn.
	}
}

func ExampleConn_Request() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	nc.Subscribe("foo", func(m *nats.Msg) {
		nc.Publish(m.Reply, []byte("I will help you"))
	})
	nc.Request("foo", []byte("help"), 50*time.Millisecond)
}

func ExampleConn_QueueSubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	received := 0

	nc.QueueSubscribe("foo", "worker_group", func(_ *nats.Msg) {
		received += 1
	})
}

func ExampleSubscription_AutoUnsubscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	defer nc.Close()

	received, wanted, total := 0, 10, 100

	sub, _ := nc.Subscribe("foo", func(_ *nats.Msg) {
		received += 1
	})
	sub.AutoUnsubscribe(wanted)

	for i := 0; i < total; i++ {
		nc.Publish("foo", []byte("Hello"))
	}
	nc.Flush()

	fmt.Printf("Received = %d", received)
}

func ExampleConn_Close() {
	nc, _ := nats.Connect(nats.DefaultURL)
	nc.Close()
}

// Shows how to wrap a Conn into an EncodedConn
func ExampleNewEncodedConn() {
	nc, _ := nats.Connect(nats.DefaultURL)
	c, _ := nats.NewEncodedConn(nc, "json")
	c.Close()
}

// EncodedConn can publish virtually anything just
// by passing it in. The encoder will be used to properly
// encode the raw Go type
func ExampleEncodedConn_Publish() {
	nc, _ := nats.Connect(nats.DefaultURL)
	c, _ := nats.NewEncodedConn(nc, "json")
	defer c.Close()

	type person struct {
		Name    string
		Address string
		Age     int
	}

	me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
	c.Publish("hello", me)
}

// EncodedConn's subscribers will automatically decode the
// wire data into the requested Go type using the Decode()
// method of the registered Encoder. The callback signature
// can also vary to include additional data, such as subject
// and reply subjects.
func ExampleEncodedConn_Subscribe() {
	nc, _ := nats.Connect(nats.DefaultURL)
	c, _ := nats.NewEncodedConn(nc, "json")
	defer c.Close()

	type person struct {
		Name    string
		Address string
		Age     int
	}

	c.Subscribe("hello", func(p *person) {
		fmt.Printf("Received a person! %+v\n", p)
	})

	c.Subscribe("hello", func(subj, reply string, p *person) {
		fmt.Printf("Received a person on subject %s! %+v\n", subj, p)
	})

	me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
	c.Publish("hello", me)
}
<|endoftext|>


<commit_before>package main

import (
	"log"

	"github.com/netrack/net/l2"
	"github.com/netrack/openflow"
	"github.com/netrack/openflow/ofp.v13"
)

func main() {
	of.HandleFunc(of.TypeHello, func(rw of.ResponseWriter, r *of.Request) {
		log.Println("RECV ofp_hello:", r.Header)
		defer log.Println("SEND ofp_hello")
		rw.WriteHeader(&of.Header{Type: of.TypeHello})
	})

	of.HandleFunc(of.TypePacketIn, func(rw of.ResponseWriter, r *of.Request) {
		var p ofp.PacketIn
		var e l2.EthernetII

		p.ReadFrom(r.Body)
		e.ReadFrom(r.Body)

		log.Println("RECV ofp_packet_in:", r.Header)
		log.Println("RECV ofp_packet_in:", e.HWDst, e.HWSrc)

		instr := ofp.Instructions{ofp.InstructionActions{
			ofp.IT_APPLY_ACTIONS,
			ofp.Actions{ofp.ActionOutput{ofp.P_FLOOD, 0}},
		}}

		fmod := &ofp.FlowMod{
			Command:      ofp.FC_ADD,
			BufferID:     p.BufferID,
			Match:        p.Match,
			Instructions: instr,
		}

		rw.Write(fmod.Bytes())
		rw.WriteHeader(&of.Header{Type: of.TypeFlowMod})
		log.Println("SEND ofp_of_mod")
	})

	of.HandleFunc(of.TypeEchoRequest, func(rw of.ResponseWriter, r *of.Request) {
		log.Println("RECV ofp_echo_request:", r.Header)
		defer log.Println("SEND ofp_echo_reply")
		rw.WriteHeader(&of.Header{Type: of.TypeEchoReply})
	})

	log.Println("started listening...")
	of.ListenAndServe(":6633", nil)
}
<commit_msg>Remove examples sub-package (#39)<commit_after><|endoftext|>


<commit_before>// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package networking

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"strings"
	"syscall"

	"github.com/appc/spec/schema/types"
	"github.com/containernetworking/cni/pkg/ns"
	cnitypes "github.com/containernetworking/cni/pkg/types"
	"github.com/hashicorp/errwrap"
	"github.com/vishvananda/netlink"

	"github.com/coreos/rkt/common"
	"github.com/coreos/rkt/networking/netinfo"
	"github.com/coreos/rkt/pkg/log"

	"golang.org/x/sys/unix"
)

const (
	IfNamePattern = "eth%d"
	selfNetNS     = "/proc/self/ns/net"
)

// Networking describes the networking details of a pod.
type Networking struct {
	podEnv

	nets []activeNet
}

// NetConf local struct extends cnitypes.NetConf with information about masquerading
// similar to CNI plugins
type NetConf struct {
	cnitypes.NetConf
	IPMasq bool `json:"ipMasq"`
	MTU    int  `json:"mtu"`
}

var stderr *log.Logger

// Setup creates a new networking namespace and executes network plugins to
// set up networking. It returns in the new pod namespace
func Setup(podRoot string, podID types.UUID, fps []ForwardedPort, netList common.NetList, localConfig, flavor string, debug bool) (*Networking, error) {

	stderr = log.New(os.Stderr, "networking", debug)

	if flavor == "kvm" {
		return kvmSetup(podRoot, podID, fps, netList, localConfig)
	}

	// Create namespace for Pod and write path to a file
	podNS, err := ns.NewNS()
	if err != nil {
		return nil, err
	}

	// TODO(jonboulle): currently podRoot is _always_ ".", and behaviour in other
	// circumstances is untested. This should be cleaned up.
	n := Networking{
		podEnv: podEnv{
			podRoot:      podRoot,
			podID:        podID,
			netsLoadList: netList,
			localConfig:  localConfig,
			podNS:        podNS,
		},
	}

	n.nets, err = n.loadNets()
	if err != nil {
		return nil, errwrap.Wrap(errors.New("error loading network definitions"), err)
	}

	if err := n.setupNets(n.nets); err != nil {
		return nil, err
	}

	if len(fps) > 0 {
		if err = n.enableDefaultLocalnetRouting(); err != nil {
			return nil, err
		}
		podIP, err := n.GetForwardableNetPodIP()
		if err != nil {
			return nil, err
		}
		if err := n.forwardPorts(fps, podIP); err != nil {
			n.unforwardPorts()
			return nil, err
		}
	}

	// Switch to the podNS
	if err := podNS.Set(); err != nil {
		return nil, err
	}

	if err = loUp(); err != nil {
		return nil, err
	}

	return &n, nil
}

// enableDefaultLocalnetRouting enables the route_localnet attribute on the supposedly default network interface.
// This allows setting up loopback NAT so the host can access the pod's forwarded ports on the localhost address.
func (n *Networking) enableDefaultLocalnetRouting() error {
	routeLocalnetFormat := ""

	defaultHostIP, err := n.GetForwardableNetHostIP()
	if err != nil {
		return err
	}

	defaultHostIPstring := defaultHostIP.String()
	switch {
	case strings.Contains(defaultHostIPstring, "."):
		routeLocalnetFormat = "/proc/sys/net/ipv4/conf/%s/route_localnet"
	case strings.Contains(defaultHostIPstring, ":"):
		return fmt.Errorf("unexpected IPv6 Address returned for default host interface: %q", defaultHostIPstring)
	default:
		return fmt.Errorf("unknown type for default Host IP: %q", defaultHostIPstring)
	}

	hostIfaces, err := n.GetIfacesByIP(defaultHostIP)
	if err != nil {
		return err
	}

	for _, hostIface := range hostIfaces {
		routeLocalnetPath := fmt.Sprintf(routeLocalnetFormat, hostIface.Name)
		routeLocalnetValue, err := ioutil.ReadFile(routeLocalnetPath)
		if err != nil {
			return err
		}
		if string(routeLocalnetValue) != "1" {
			routeLocalnetFile, err := os.OpenFile(routeLocalnetPath, os.O_WRONLY, 0)
			if err != nil {
				return err
			}
			defer routeLocalnetFile.Close()

			if _, err = io.WriteString(routeLocalnetFile, "1"); err != nil {
				return err
			}
		}
	}

	return nil
}

// Load creates the Networking object from saved state.
// Assumes the current netns is that of the host.
func Load(podRoot string, podID *types.UUID) (*Networking, error) {
	// the current directory is pod root
	pdirfd, err := syscall.Open(podRoot, syscall.O_RDONLY|syscall.O_DIRECTORY, 0)
	if err != nil {
		return nil, errwrap.Wrap(fmt.Errorf("failed to open pod root directory (%v)", podRoot), err)
	}
	defer syscall.Close(pdirfd)

	nis, err := netinfo.LoadAt(pdirfd)
	if err != nil {
		return nil, err
	}

	var nets []activeNet
	for _, ni := range nis {
		n, err := loadNet(ni.ConfPath)
		if err != nil {
			if !os.IsNotExist(err) {
				stderr.PrintE(fmt.Sprintf("error loading %q; ignoring", ni.ConfPath), err)
			}
			continue
		}

		// make a copy of ni to make it a unique object as it's saved via ptr
		rti := ni
		n.runtime = &rti
		nets = append(nets, *n)
	}

	p := podEnv{
		podRoot: podRoot,
		podID:   *podID,
	}

	podNS, err := p.podNSLoad()
	if err != nil {
		return nil, err
	}
	p.podNS = podNS

	return &Networking{
		podEnv: p,
		nets:   nets,
	}, nil
}

// GetIfacesByIP searches for and returns the interfaces with the given IP
// Disregards the subnet mask since not every net.IP object contains
// On success it will return the list of found interfaces
func (n *Networking) GetIfacesByIP(ifaceIP net.IP) ([]net.Interface, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}

	searchAddr := strings.Split(ifaceIP.String(), "/")[0]
	resultInterfaces := make([]net.Interface, 0)

	for _, iface := range ifaces {
		if iface.Flags&net.FlagLoopback != 0 {
			continue
		}

		addrs, err := iface.Addrs()
		if err != nil {
			return nil, errwrap.Wrap(fmt.Errorf("cannot get addresses for interface %v", iface.Name), err)
		}

		for _, addr := range addrs {
			currentAddr := strings.Split(addr.String(), "/")[0]
			if searchAddr == currentAddr {
				resultInterfaces = append(resultInterfaces, iface)
				break
			}
		}
	}

	if len(resultInterfaces) == 0 {
		return nil, fmt.Errorf("no interface found with IP %q", ifaceIP)
	}

	return resultInterfaces, nil
}

// Teardown cleans up a produced Networking object.
func (n *Networking) Teardown(flavor string, debug bool) {

	stderr = log.New(os.Stderr, "networking", debug)

	// Teardown everything in reverse order of setup.
	// This should be idempotent -- be tolerant of missing stuff

	if flavor == "kvm" {
		n.kvmTeardown()
		return
	}

	if err := n.unforwardPorts(); err != nil {
		stderr.PrintE("error removing forwarded ports", err)
	}

	podNS, err := n.podNSLoad()
	if err != nil {
		stderr.PrintE("error loading podNS: %v", err)
	}
	if podNS != nil {
		defer func() {
			n.podNS.Close()

			if err := syscall.Unmount(n.podNS.Path(), unix.MNT_DETACH); err != nil {
				// if already unmounted, umount(2) returns EINVAL
				if !os.IsNotExist(err) && err != syscall.EINVAL {
					stderr.PrintE(fmt.Sprintf("error unmounting %q", n.podNS.Path()), err)
				}
			}

			if err := os.RemoveAll(n.podNS.Path()); err != nil {
				stderr.PrintE(fmt.Sprintf("failed to remove namespace %s", n.podNS.Path()), err)
			}
		}()
	}

	n.podNS = podNS
	n.teardownNets(n.nets)
}

// Save writes out the info about active nets
// for "rkt list" and friends to display
func (e *Networking) Save() error {

	if e.podNS != nil {
		if err := e.podNSPathSave(); err != nil {
			return err
		}
	}

	var nis []netinfo.NetInfo
	for _, n := range e.nets {
		nis = append(nis, *n.runtime)
	}

	return netinfo.Save(e.podRoot, nis)
}

func loUp() error {
	lo, err := netlink.LinkByName("lo")
	if err != nil {
		return errwrap.Wrap(errors.New("failed to lookup lo"), err)
	}

	if err := netlink.LinkSetUp(lo); err != nil {
		return errwrap.Wrap(errors.New("failed to set lo up"), err)
	}

	return nil
}
<commit_msg>networking: remove superfluous %v printf verb in stderr.PrintE("error loading podNS: %v", err) in networking.go<commit_after>// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package networking

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"os"
	"strings"
	"syscall"

	"github.com/appc/spec/schema/types"
	"github.com/containernetworking/cni/pkg/ns"
	cnitypes "github.com/containernetworking/cni/pkg/types"
	"github.com/hashicorp/errwrap"
	"github.com/vishvananda/netlink"

	"github.com/coreos/rkt/common"
	"github.com/coreos/rkt/networking/netinfo"
	"github.com/coreos/rkt/pkg/log"

	"golang.org/x/sys/unix"
)

const (
	IfNamePattern = "eth%d"
	selfNetNS     = "/proc/self/ns/net"
)

// Networking describes the networking details of a pod.
type Networking struct {
	podEnv

	nets []activeNet
}

// NetConf local struct extends cnitypes.NetConf with information about masquerading
// similar to CNI plugins
type NetConf struct {
	cnitypes.NetConf
	IPMasq bool `json:"ipMasq"`
	MTU    int  `json:"mtu"`
}

var stderr *log.Logger

// Setup creates a new networking namespace and executes network plugins to
// set up networking. It returns in the new pod namespace
func Setup(podRoot string, podID types.UUID, fps []ForwardedPort, netList common.NetList, localConfig, flavor string, debug bool) (*Networking, error) {

	stderr = log.New(os.Stderr, "networking", debug)

	if flavor == "kvm" {
		return kvmSetup(podRoot, podID, fps, netList, localConfig)
	}

	// Create namespace for Pod and write path to a file
	podNS, err := ns.NewNS()
	if err != nil {
		return nil, err
	}

	// TODO(jonboulle): currently podRoot is _always_ ".", and behaviour in other
	// circumstances is untested. This should be cleaned up.
	n := Networking{
		podEnv: podEnv{
			podRoot:      podRoot,
			podID:        podID,
			netsLoadList: netList,
			localConfig:  localConfig,
			podNS:        podNS,
		},
	}

	n.nets, err = n.loadNets()
	if err != nil {
		return nil, errwrap.Wrap(errors.New("error loading network definitions"), err)
	}

	if err := n.setupNets(n.nets); err != nil {
		return nil, err
	}

	if len(fps) > 0 {
		if err = n.enableDefaultLocalnetRouting(); err != nil {
			return nil, err
		}
		podIP, err := n.GetForwardableNetPodIP()
		if err != nil {
			return nil, err
		}
		if err := n.forwardPorts(fps, podIP); err != nil {
			n.unforwardPorts()
			return nil, err
		}
	}

	// Switch to the podNS
	if err := podNS.Set(); err != nil {
		return nil, err
	}

	if err = loUp(); err != nil {
		return nil, err
	}

	return &n, nil
}

// enableDefaultLocalnetRouting enables the route_localnet attribute on the supposedly default network interface.
// This allows setting up loopback NAT so the host can access the pod's forwarded ports on the localhost address.
func (n *Networking) enableDefaultLocalnetRouting() error {
	routeLocalnetFormat := ""

	defaultHostIP, err := n.GetForwardableNetHostIP()
	if err != nil {
		return err
	}

	defaultHostIPstring := defaultHostIP.String()
	switch {
	case strings.Contains(defaultHostIPstring, "."):
		routeLocalnetFormat = "/proc/sys/net/ipv4/conf/%s/route_localnet"
	case strings.Contains(defaultHostIPstring, ":"):
		return fmt.Errorf("unexpected IPv6 Address returned for default host interface: %q", defaultHostIPstring)
	default:
		return fmt.Errorf("unknown type for default Host IP: %q", defaultHostIPstring)
	}

	hostIfaces, err := n.GetIfacesByIP(defaultHostIP)
	if err != nil {
		return err
	}

	for _, hostIface := range hostIfaces {
		routeLocalnetPath := fmt.Sprintf(routeLocalnetFormat, hostIface.Name)
		routeLocalnetValue, err := ioutil.ReadFile(routeLocalnetPath)
		if err != nil {
			return err
		}
		if string(routeLocalnetValue) != "1" {
			routeLocalnetFile, err := os.OpenFile(routeLocalnetPath, os.O_WRONLY, 0)
			if err != nil {
				return err
			}
			defer routeLocalnetFile.Close()

			if _, err = io.WriteString(routeLocalnetFile, "1"); err != nil {
				return err
			}
		}
	}

	return nil
}

// Load creates the Networking object from saved state.
// Assumes the current netns is that of the host.
func Load(podRoot string, podID *types.UUID) (*Networking, error) {
	// the current directory is pod root
	pdirfd, err := syscall.Open(podRoot, syscall.O_RDONLY|syscall.O_DIRECTORY, 0)
	if err != nil {
		return nil, errwrap.Wrap(fmt.Errorf("failed to open pod root directory (%v)", podRoot), err)
	}
	defer syscall.Close(pdirfd)

	nis, err := netinfo.LoadAt(pdirfd)
	if err != nil {
		return nil, err
	}

	var nets []activeNet
	for _, ni := range nis {
		n, err := loadNet(ni.ConfPath)
		if err != nil {
			if !os.IsNotExist(err) {
				stderr.PrintE(fmt.Sprintf("error loading %q; ignoring", ni.ConfPath), err)
			}
			continue
		}

		// make a copy of ni to make it a unique object as it's saved via ptr
		rti := ni
		n.runtime = &rti
		nets = append(nets, *n)
	}

	p := podEnv{
		podRoot: podRoot,
		podID:   *podID,
	}

	podNS, err := p.podNSLoad()
	if err != nil {
		return nil, err
	}
	p.podNS = podNS

	return &Networking{
		podEnv: p,
		nets:   nets,
	}, nil
}

// GetIfacesByIP searches for and returns the interfaces with the given IP
// Disregards the subnet mask since not every net.IP object contains
// On success it will return the list of found interfaces
func (n *Networking) GetIfacesByIP(ifaceIP net.IP) ([]net.Interface, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}

	searchAddr := strings.Split(ifaceIP.String(), "/")[0]
	resultInterfaces := make([]net.Interface, 0)

	for _, iface := range ifaces {
		if iface.Flags&net.FlagLoopback != 0 {
			continue
		}

		addrs, err := iface.Addrs()
		if err != nil {
			return nil, errwrap.Wrap(fmt.Errorf("cannot get addresses for interface %v", iface.Name), err)
		}

		for _, addr := range addrs {
			currentAddr := strings.Split(addr.String(), "/")[0]
			if searchAddr == currentAddr {
				resultInterfaces = append(resultInterfaces, iface)
				break
			}
		}
	}

	if len(resultInterfaces) == 0 {
		return nil, fmt.Errorf("no interface found with IP %q", ifaceIP)
	}

	return resultInterfaces, nil
}

// Teardown cleans up a produced Networking object.
func (n *Networking) Teardown(flavor string, debug bool) {

	stderr = log.New(os.Stderr, "networking", debug)

	// Teardown everything in reverse order of setup.
	// This should be idempotent -- be tolerant of missing stuff

	if flavor == "kvm" {
		n.kvmTeardown()
		return
	}

	if err := n.unforwardPorts(); err != nil {
		stderr.PrintE("error removing forwarded ports", err)
	}

	podNS, err := n.podNSLoad()
	if err != nil {
		stderr.PrintE("error loading podNS", err)
	}
	if podNS != nil {
		defer func() {
			n.podNS.Close()

			if err := syscall.Unmount(n.podNS.Path(), unix.MNT_DETACH); err != nil {
				// if already unmounted, umount(2) returns EINVAL
				if !os.IsNotExist(err) && err != syscall.EINVAL {
					stderr.PrintE(fmt.Sprintf("error unmounting %q", n.podNS.Path()), err)
				}
			}

			if err := os.RemoveAll(n.podNS.Path()); err != nil {
				stderr.PrintE(fmt.Sprintf("failed to remove namespace %s", n.podNS.Path()), err)
			}
		}()
	}

	n.podNS = podNS
	n.teardownNets(n.nets)
}

// Save writes out the info about active nets
// for "rkt list" and friends to display
func (e *Networking) Save() error {

	if e.podNS != nil {
		if err := e.podNSPathSave(); err != nil {
			return err
		}
	}

	var nis []netinfo.NetInfo
	for _, n := range e.nets {
		nis = append(nis, *n.runtime)
	}

	return netinfo.Save(e.podRoot, nis)
}

func loUp() error {
	lo, err := netlink.LinkByName("lo")
	if err != nil {
		return errwrap.Wrap(errors.New("failed to lookup lo"), err)
	}

	if err := netlink.LinkSetUp(lo); err != nil {
		return errwrap.Wrap(errors.New("failed to set lo up"), err)
	}

	return nil
}
<|endoftext|>


<commit_before>package rados

import "C"

import (
	"fmt"
	"strings"
)

// The file operation.go exists to support both read op and write op types that
// have some pretty common behaviors between them. In C/C++ its assumed that
// the buffer types and other pointers will not be freed between passing them
// to the action setup calls (things like rados_write_op_write or
// rados_read_op_omap_get_vals2) and the call to Operate(...).  Since there's
// nothing stopping one from sleeping for hours between these calls, or passing
// the op to other functions and calling Operate there, we want a mechanism
// that's (fairly) simple to understand and won't run afoul of Go's garbage
// collection.  That's one reason the operation type tracks the steps (the
// parts that track complex inputs and outputs) so that as long as the op
// exists it will have a reference to the step, which will have references
// to the C language types.

type opKind string

const (
	readOp  opKind = "read"
	writeOp opKind = "write"
)

// OperationError is an error type that may be returned by an Operate call.
// It captures the error from the operate call itself and any errors from
// steps that can return an error.
type OperationError struct {
	kind       opKind
	OpError    error
	StepErrors map[int]error
}

func (e OperationError) Error() string {
	subErrors := []string{}
	if e.OpError != nil {
		subErrors = append(subErrors,
			fmt.Sprintf("op=%s", e.OpError))
	}
	for idx, es := range e.StepErrors {
		subErrors = append(subErrors,
			fmt.Sprintf("Step#%d=%s", idx, es))
	}
	return fmt.Sprintf(
		"%s operation error: %s",
		e.kind,
		strings.Join(subErrors, ", "))
}

// opStep provides an interface for types that are tied to the management of
// data being input or output from write ops and read ops. The steps are
// meant to simplify the internals of the ops themselves and be exportable when
// appropriate. If a step is not being exported it should not be returned
// from an ops action function. If the step is exported it should be
// returned from an ops action function.
//
// Not all types implementing opStep are expected to need all the functions
// in the interface. However, for the sake of simplicity on the op side, we use
// the same interface for all cases and expect those implementing opStep
// just embed the without* types that provide no-op implementation of
// functions that make up this interface.
type opStep interface {
	// update the state of the step after the call to Operate.
	// It can be used to convert values from C and cache them and/or
	// communicate a failure of the action associated with the step.  The
	// update call will only be made once. Implementations are not required to
	// handle this call being made more than once.
	update() error
	// free will be called to free any resources, especially C memory, that
	// the step is managing. The behavior of free should be idempotent and
	// handle being called more than once.
	free()
}

// operation represents some of the shared underlying mechanisms for
// both read and write op types.
type operation struct {
	steps []opStep
}

// free will call the free method of all the steps this operation
// contains.
func (o *operation) free() {
	for i := range o.steps {
		o.steps[i].free()
	}
}

// update the operation and the steps it contains. The top-level result
// of the rados call is passed in as ret and used to construct errors.
// The update call of each step is used to update the contents of each
// step and gather any errors from those steps.
func (o *operation) update(kind opKind, ret C.int) error {
	stepErrors := map[int]error{}
	for i := range o.steps {
		if err := o.steps[i].update(); err != nil {
			stepErrors[i] = err
		}
	}
	if ret == 0 && len(stepErrors) == 0 {
		return nil
	}
	return OperationError{
		kind:       kind,
		OpError:    getError(ret),
		StepErrors: stepErrors,
	}
}

func opStepFinalizer(s opStep) {
	if s != nil {
		s.free()
	}
}

// withoutUpdate can be embedded in a struct to help indicate
// the type implements the opStep interface but has a no-op
// update function.
type withoutUpdate struct{}

func (*withoutUpdate) update() error { return nil }

// withoutFree can be embedded in a struct to help indicate
// the type implements the opStep interface but has a no-op
// free function.
type withoutFree struct{}

func (*withoutFree) free() {}
<commit_msg>rados: add withRefs type<commit_after>package rados

// #include <stdlib.h>
import "C"

import (
	"fmt"
	"strings"
	"unsafe"
)

// The file operation.go exists to support both read op and write op types that
// have some pretty common behaviors between them. In C/C++ its assumed that
// the buffer types and other pointers will not be freed between passing them
// to the action setup calls (things like rados_write_op_write or
// rados_read_op_omap_get_vals2) and the call to Operate(...).  Since there's
// nothing stopping one from sleeping for hours between these calls, or passing
// the op to other functions and calling Operate there, we want a mechanism
// that's (fairly) simple to understand and won't run afoul of Go's garbage
// collection.  That's one reason the operation type tracks the steps (the
// parts that track complex inputs and outputs) so that as long as the op
// exists it will have a reference to the step, which will have references
// to the C language types.

type opKind string

const (
	readOp  opKind = "read"
	writeOp opKind = "write"
)

// OperationError is an error type that may be returned by an Operate call.
// It captures the error from the operate call itself and any errors from
// steps that can return an error.
type OperationError struct {
	kind       opKind
	OpError    error
	StepErrors map[int]error
}

func (e OperationError) Error() string {
	subErrors := []string{}
	if e.OpError != nil {
		subErrors = append(subErrors,
			fmt.Sprintf("op=%s", e.OpError))
	}
	for idx, es := range e.StepErrors {
		subErrors = append(subErrors,
			fmt.Sprintf("Step#%d=%s", idx, es))
	}
	return fmt.Sprintf(
		"%s operation error: %s",
		e.kind,
		strings.Join(subErrors, ", "))
}

// opStep provides an interface for types that are tied to the management of
// data being input or output from write ops and read ops. The steps are
// meant to simplify the internals of the ops themselves and be exportable when
// appropriate. If a step is not being exported it should not be returned
// from an ops action function. If the step is exported it should be
// returned from an ops action function.
//
// Not all types implementing opStep are expected to need all the functions
// in the interface. However, for the sake of simplicity on the op side, we use
// the same interface for all cases and expect those implementing opStep
// just embed the without* types that provide no-op implementation of
// functions that make up this interface.
type opStep interface {
	// update the state of the step after the call to Operate.
	// It can be used to convert values from C and cache them and/or
	// communicate a failure of the action associated with the step.  The
	// update call will only be made once. Implementations are not required to
	// handle this call being made more than once.
	update() error
	// free will be called to free any resources, especially C memory, that
	// the step is managing. The behavior of free should be idempotent and
	// handle being called more than once.
	free()
}

// operation represents some of the shared underlying mechanisms for
// both read and write op types.
type operation struct {
	steps []opStep
}

// free will call the free method of all the steps this operation
// contains.
func (o *operation) free() {
	for i := range o.steps {
		o.steps[i].free()
	}
}

// update the operation and the steps it contains. The top-level result
// of the rados call is passed in as ret and used to construct errors.
// The update call of each step is used to update the contents of each
// step and gather any errors from those steps.
func (o *operation) update(kind opKind, ret C.int) error {
	stepErrors := map[int]error{}
	for i := range o.steps {
		if err := o.steps[i].update(); err != nil {
			stepErrors[i] = err
		}
	}
	if ret == 0 && len(stepErrors) == 0 {
		return nil
	}
	return OperationError{
		kind:       kind,
		OpError:    getError(ret),
		StepErrors: stepErrors,
	}
}

func opStepFinalizer(s opStep) {
	if s != nil {
		s.free()
	}
}

// withoutUpdate can be embedded in a struct to help indicate
// the type implements the opStep interface but has a no-op
// update function.
type withoutUpdate struct{}

func (*withoutUpdate) update() error { return nil }

// withoutFree can be embedded in a struct to help indicate
// the type implements the opStep interface but has a no-op
// free function.
type withoutFree struct{}

func (*withoutFree) free() {}

// withRefs is a embeddable type to help track and free C memory.
type withRefs struct {
	refs []unsafe.Pointer
}

func (w *withRefs) free() {
	for i := range w.refs {
		C.free(w.refs[i])
	}
	w.refs = nil
}

func (w *withRefs) add(ptr unsafe.Pointer) {
	w.refs = append(w.refs, ptr)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"github.com/bitly/nsq/nsq"
	"log"
	"net"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"
)

var (
	topic            = flag.String("topic", "", "nsq topic")
	channel          = flag.String("channel", "nsq_to_tcp", "nsq channel")
	maxInFlight      = flag.Int("max-in-flight", 200, "max number of messages to allow in flight")
	lookupdHTTPAddrs = flag.String("lookupd-http-address", "127.0.0.1:4161", "lookupd http")
	port             = flag.String("port", ":1514", "log send port")
)

type MsgHandler struct {
	msg_chan chan []byte
}

func (this *MsgHandler) HandleMessage(m *nsq.Message) error {
	this.msg_chan <- m.Body
	return nil
}

func main() {
	flag.Parse()

	if *topic == "" || *channel == "" {
		log.Fatalf("--topic and --channel are required")
	}

	if *maxInFlight < 0 {
		log.Fatalf("--max-in-flight must be > 0")
	}

	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

	r, err := nsq.NewReader(*topic, *channel)
	if err != nil {
		log.Fatalf(err.Error())
	}
	r.SetMaxInFlight(*maxInFlight)
	msg_handler := MsgHandler{make(chan []byte)}
	r.AddHandler(&msg_handler)
	lookupdlist := strings.Split(*lookupdHTTPAddrs, ",")
	exitchan := make(chan int)
	go tcp_server(*port, msg_handler.msg_chan, exitchan)
	for _, addrString := range lookupdlist {
		log.Printf("lookupd addr %s", addrString)
		err := r.ConnectToLookupd(addrString)
		if err != nil {
			log.Fatalf(err.Error())
		}
	}

	select {
	case <-r.ExitChan:
		log.Println("reader exited")
	case <-sigChan:
		r.Stop()
		exitchan <- 1
	}
	time.Sleep(time.Second)
}

func tcp_server(port string, logchan chan []byte, exitchan chan int) {
	server, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatal("server bind failed:", err)
		return
	}
	go func() {
		for {
			fd, err := server.Accept()
			if err != nil &&
				strings.Contains(err.Error(),
					"use of closed network connection") {
				break
			}
			if err != nil {
				log.Fatal("accept error", err)
				time.Sleep(time.Second)
			} else {
				go send_log(fd, logchan)
			}
		}
	}()
	<-exitchan
	server.Close()
	log.Println("tcp server closed")
}

func send_log(fd net.Conn, logchan chan []byte) {
	defer fd.Close()
	var err error
	for {
		msg, ok := <-logchan
		if !ok {
			break
		}
		_, err = fd.Write(msg)
		if err != nil {
			log.Fatal("write failed ", err)
			break
		}
	}
}
<commit_msg>fix logic<commit_after>package main

import (
	"flag"
	"github.com/bitly/nsq/nsq"
	"log"
	"net"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"
)

var (
	topic            = flag.String("topic", "", "nsq topic")
	channel          = flag.String("channel", "nsq_to_tcp", "nsq channel")
	maxInFlight      = flag.Int("max-in-flight", 200, "max number of messages to allow in flight")
	lookupdHTTPAddrs = flag.String("lookupd-http-address", "127.0.0.1:4161", "lookupd http")
	port             = flag.String("port", ":1514", "log send port")
)

type MsgHandler struct {
	msg_chan chan []byte
}

func (this *MsgHandler) HandleMessage(m *nsq.Message) error {
	this.msg_chan <- m.Body
	return nil
}

func main() {
	flag.Parse()

	if *topic == "" || *channel == "" {
		log.Fatalf("--topic and --channel are required")
	}

	if *maxInFlight < 0 {
		log.Fatalf("--max-in-flight must be > 0")
	}

	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

	r, err := nsq.NewReader(*topic, *channel)
	if err != nil {
		log.Fatalf(err.Error())
	}
	r.SetMaxInFlight(*maxInFlight)
	msg_handler := MsgHandler{make(chan []byte)}
	r.AddHandler(&msg_handler)
	lookupdlist := strings.Split(*lookupdHTTPAddrs, ",")
	exitchan := make(chan int)
	go tcp_server(*port, msg_handler.msg_chan, exitchan)
	for _, addrString := range lookupdlist {
		log.Printf("lookupd addr %s", addrString)
		err := r.ConnectToLookupd(addrString)
		if err != nil {
			log.Fatalf(err.Error())
		}
	}

	select {
	case <-r.ExitChan:
		log.Println("reader exited")
	case <-sigChan:
		r.Stop()
		log.Println("stop nsq reader")
	}
	time.Sleep(time.Second)
}

func tcp_server(port string, logchan chan []byte, exitchan chan int) {
	server, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatal("server bind failed:", err)
		return
	}
	go func() {
		for {
			fd, err := server.Accept()
			if err != nil &&
				strings.Contains(err.Error(),
					"use of closed network connection") {
				break
			}
			if err != nil {
				log.Fatal("accept error", err)
				time.Sleep(time.Second)
			} else {
				go send_log(fd, logchan)
			}
		}
	}()
	<-exitchan
	server.Close()
	log.Println("tcp server closed")
}

func send_log(fd net.Conn, logchan chan []byte) {
	defer fd.Close()
	var err error
	for {
		msg, ok := <-logchan
		if !ok {
			break
		}
		_, err = fd.Write(msg)
		if err != nil {
			log.Fatal("write failed ", err)
			break
		}
	}
}
<|endoftext|>


<commit_before>package options

import (
	"flag"
	"fmt"
	"github.com/BurntSushi/toml"
	"log"
	"os"
)

type NodeConf struct {
	Seeds                 []string `toml:"seed_servers"`
	UpdateInterval        string   `toml:"update_interval"`
	HeartbeatInterval     string   `toml:"heartbeat_interval"`
	MaxMissedBeats        int      `toml:"max_missed_beats"`
	IgnoreVersionMismatch bool     `toml:"node_ignore_version_mismatch"`
}

type Conf struct {
	Root              string   `toml:"root_dir"`
	ApiPort           string   `toml:"api_port"`
	RunNode           bool     `toml:"run_as_node"`
	NodeConfig        NodeConf `toml:"node"`
	Cores             int      `toml:"cores"`
	Seed              string   `toml:"seed"`
	Cert              string   `toml:"tls_cert"`
	Key               string   `toml:"tls_key"`
	Ssl               bool     `toml:"use_ssl"`
	AuthorizedClients []string `toml:"authorized_clients"`
}

var Config Conf

func GetOptions() {
	var configFile string

	flag.StringVar(&configFile, "config", "", "Configuration file")
	flag.IntVar(&Config.Cores, "cores", 2, "Amount of cores to pass to GOMAXPROC (experimental)")
	flag.StringVar(&Config.Root, "root", "", "Root directory to serve (required). Must be absolute path")
	flag.StringVar(&Config.ApiPort, "api-port", "8081", "Port that the API listens on")
	flag.StringVar(&Config.Seed, "seed", "", "Seed server to query")
	flag.BoolVar(&Config.RunNode, "node", false, "Run as a node")
	flag.StringVar(&Config.Cert, "tls-cert", "", "Path to TLS certificate to use")
	flag.StringVar(&Config.Key, "tls-key", "", "Path to TLS key to use")
	flag.BoolVar(&Config.Ssl, "ssl", true, "Use TLS/SSL")

	flag.IntVar(&Config.NodeConfig.MaxMissedBeats, "missed-beats", 4, "How many heartbeats the server can miss before the node goes offline")
	flag.StringVar(&Config.NodeConfig.HeartbeatInterval, "heartbeat-interval", "30s", "How often to send a heartbeat to the server")
	flag.StringVar(&Config.NodeConfig.UpdateInterval, "update-interval", "1m", "How often to update with the other servers")
	flag.BoolVar(&Config.NodeConfig.IgnoreVersionMismatch, "node-ignore-version-mismatch", false,
		"Ignore a mismatch in server and client versions")

	flag.Parse()

	log.Println(Config.AuthorizedClients)
	if configFile != "" {
		if _, err := toml.DecodeFile(configFile, &Config); err != nil {
			fmt.Printf("Error reading config %s: %s\n", configFile, err.Error())
			os.Exit(-1)
		}
		fmt.Printf("Configration file options in %s overriding command line options\n", configFile)
	}

	if Config.Root == "" {
		fmt.Println("Must specify root directory")
		os.Exit(-1)
	}

	if Config.RunNode == true && Config.NodeConfig.Seeds == nil && Config.Seed == "" {
		fmt.Println("Must specify seed server when running as node")
		os.Exit(-1)
	} else if Config.RunNode == true && Config.NodeConfig.Seeds == nil && Config.Seed != "" {
		Config.NodeConfig.Seeds = append(Config.NodeConfig.Seeds, Config.Seed)
	}
}
<commit_msg>Stale code cleanup<commit_after>package options

import (
	"flag"
	"fmt"
	"github.com/BurntSushi/toml"
	"os"
)

type NodeConf struct {
	Seeds                 []string `toml:"seed_servers"`
	UpdateInterval        string   `toml:"update_interval"`
	HeartbeatInterval     string   `toml:"heartbeat_interval"`
	MaxMissedBeats        int      `toml:"max_missed_beats"`
	IgnoreVersionMismatch bool     `toml:"node_ignore_version_mismatch"`
}

type Conf struct {
	Root       string   `toml:"root_dir"`
	ApiPort    string   `toml:"api_port"`
	RunNode    bool     `toml:"run_as_node"`
	NodeConfig NodeConf `toml:"node"`
	Cores      int      `toml:"cores"`
	Seed       string   `toml:"seed"`
	Cert       string   `toml:"tls_cert"`
	Key        string   `toml:"tls_key"`
	Ssl        bool     `toml:"use_ssl"`
}

var Config Conf

func GetOptions() {
	var configFile string

	flag.StringVar(&configFile, "config", "", "Configuration file")
	flag.IntVar(&Config.Cores, "cores", 2, "Amount of cores to pass to GOMAXPROC (experimental)")
	flag.StringVar(&Config.Root, "root", "", "Root directory to serve (required). Must be absolute path")
	flag.StringVar(&Config.ApiPort, "api-port", "8081", "Port that the API listens on")
	flag.StringVar(&Config.Seed, "seed", "", "Seed server to query")
	flag.BoolVar(&Config.RunNode, "node", false, "Run as a node")
	flag.StringVar(&Config.Cert, "tls-cert", "", "Path to TLS certificate to use")
	flag.StringVar(&Config.Key, "tls-key", "", "Path to TLS key to use")
	flag.BoolVar(&Config.Ssl, "ssl", true, "Use TLS/SSL")

	flag.IntVar(&Config.NodeConfig.MaxMissedBeats, "missed-beats", 4, "How many heartbeats the server can miss before the node goes offline")
	flag.StringVar(&Config.NodeConfig.HeartbeatInterval, "heartbeat-interval", "30s", "How often to send a heartbeat to the server")
	flag.StringVar(&Config.NodeConfig.UpdateInterval, "update-interval", "1m", "How often to update with the other servers")
	flag.BoolVar(&Config.NodeConfig.IgnoreVersionMismatch, "node-ignore-version-mismatch", false,
		"Ignore a mismatch in server and client versions")

	flag.Parse()

	if configFile != "" {
		if _, err := toml.DecodeFile(configFile, &Config); err != nil {
			fmt.Printf("Error reading config %s: %s\n", configFile, err.Error())
			os.Exit(-1)
		}
		fmt.Printf("Configration file options in %s overriding command line options\n", configFile)
	}

	if Config.Root == "" {
		fmt.Println("Must specify root directory")
		os.Exit(-1)
	}

	if Config.RunNode == true && Config.NodeConfig.Seeds == nil && Config.Seed == "" {
		fmt.Println("Must specify seed server when running as node")
		os.Exit(-1)
	} else if Config.RunNode == true && Config.NodeConfig.Seeds == nil && Config.Seed != "" {
		Config.NodeConfig.Seeds = append(Config.NodeConfig.Seeds, Config.Seed)
	}
}
<|endoftext|>


<commit_before>package atlas

import (
	"fmt"
)

// A type to enumerate key sorting modes.
type KeySortMode string

const (
	KeySortMode_Default = "default" // e.g. lexical string sort for strings, etc
	KeySortMode_RFC7049 = "rfc7049" // "Canonical" as proposed by rfc7049 § 3.9 (shorter byte sequences sort to top)
)

type MapMorphism struct {
	KeySortMode KeySortMode
}

func (x *BuilderCore) MapMorphism() *BuilderMapMorphism {
	x.entry.MapMorphism = &MapMorphism{}
	return &BuilderMapMorphism{x.entry}
}

type BuilderMapMorphism struct {
	entry *AtlasEntry
}

func (x *BuilderMapMorphism) Complete() *AtlasEntry {
	return x.entry
}

func (x *BuilderMapMorphism) SetKeySortMode(km KeySortMode) *BuilderMapMorphism {
	switch km {
	case KeySortMode_Default, KeySortMode_RFC7049:
		x.entry.MapMorphism.KeySortMode = km
	default:
		panic(fmt.Errorf("invalid key sort mode %q", km))
	}
	return x
}
<commit_msg>obj/atlas: builder always sets explicit default for map sort ordering.<commit_after>package atlas

import (
	"fmt"
)

// A type to enumerate key sorting modes.
type KeySortMode string

const (
	KeySortMode_Default = "default" // e.g. lexical string sort for strings, etc
	KeySortMode_RFC7049 = "rfc7049" // "Canonical" as proposed by rfc7049 § 3.9 (shorter byte sequences sort to top)
)

type MapMorphism struct {
	KeySortMode KeySortMode
}

func (x *BuilderCore) MapMorphism() *BuilderMapMorphism {
	x.entry.MapMorphism = &MapMorphism{
		KeySortMode_Default,
	}
	return &BuilderMapMorphism{x.entry}
}

type BuilderMapMorphism struct {
	entry *AtlasEntry
}

func (x *BuilderMapMorphism) Complete() *AtlasEntry {
	return x.entry
}

func (x *BuilderMapMorphism) SetKeySortMode(km KeySortMode) *BuilderMapMorphism {
	switch km {
	case KeySortMode_Default, KeySortMode_RFC7049:
		x.entry.MapMorphism.KeySortMode = km
	default:
		panic(fmt.Errorf("invalid key sort mode %q", km))
	}
	return x
}
<|endoftext|>


<commit_before>package oauth2_test

import (
	"net/http"
	"net/http/cookiejar"
	"net/http/httptest"
	"net/url"
	"testing"
	"time"

	clog "github.com/influxdata/chronograf/log"
	"github.com/influxdata/chronograf/oauth2"
)

func Test_JWTMux_Logout_DeletesSessionCookie(t *testing.T) {
	t.Parallel()

	mp := &MockProvider{"biff@example.com"}
	jm := oauth2.NewJWTMux(mp, &YesManAuthenticator{}, clog.New(clog.ParseLevel("debug")))

	testTime := time.Date(1985, time.October, 25, 18, 0, 0, 0, time.UTC)

	jm.Now = func() time.Time {
		return testTime
	}

	ts := httptest.NewServer(jm.Logout())
	defer ts.Close()

	tsUrl, _ := url.Parse(ts.URL)

	jar, _ := cookiejar.New(nil)
	hc := http.Client{
		Jar: jar,
		CheckRedirect: func(r *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}
	hc.Jar.SetCookies(tsUrl, []*http.Cookie{
		&http.Cookie{
			Name:  oauth2.DefaultCookieName,
			Value: "",
		},
	})

	resp, err := hc.Get(ts.URL)
	if err != nil {
		t.Fatal("Error communicating with Logout() handler: err:", err)
	}

	if resp.StatusCode < 300 || resp.StatusCode >= 400 {
		t.Fatal("Expected to be redirected, but received status code", resp.StatusCode)
	}

	cookies := resp.Cookies()
	if len(cookies) != 1 {
		t.Fatal("Expected that cookie would be present but wasn't")
	}

	c := cookies[0]
	if c.Name != oauth2.DefaultCookieName || c.Expires != testTime.Add(-1*time.Hour) {
		t.Fatal("Expected cookie to be expired but wasn't")
	}
}
<commit_msg>Add test for (*JWTMux).Login()<commit_after>package oauth2_test

import (
	"net/http"
	"net/http/cookiejar"
	"net/http/httptest"
	"net/url"
	"testing"
	"time"

	clog "github.com/influxdata/chronograf/log"
	"github.com/influxdata/chronograf/oauth2"
)

var testTime time.Time = time.Date(1985, time.October, 25, 18, 0, 0, 0, time.UTC)

func Test_JWTMux_Logout_DeletesSessionCookie(t *testing.T) {
	t.Parallel()

	mp := &MockProvider{"biff@example.com"}
	jm := oauth2.NewJWTMux(mp, &YesManAuthenticator{}, clog.New(clog.ParseLevel("debug")))

	jm.Now = func() time.Time {
		return testTime
	}

	ts := httptest.NewServer(jm.Logout())
	defer ts.Close()

	tsUrl, _ := url.Parse(ts.URL)

	jar, _ := cookiejar.New(nil)
	hc := http.Client{
		Jar: jar,
		CheckRedirect: func(r *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}
	hc.Jar.SetCookies(tsUrl, []*http.Cookie{
		&http.Cookie{
			Name:  oauth2.DefaultCookieName,
			Value: "",
		},
	})

	resp, err := hc.Get(ts.URL)
	if err != nil {
		t.Fatal("Error communicating with Logout() handler: err:", err)
	}

	if resp.StatusCode < 300 || resp.StatusCode >= 400 {
		t.Fatal("Expected to be redirected, but received status code", resp.StatusCode)
	}

	cookies := resp.Cookies()
	if len(cookies) != 1 {
		t.Fatal("Expected that cookie would be present but wasn't")
	}

	c := cookies[0]
	if c.Name != oauth2.DefaultCookieName || c.Expires != testTime.Add(-1*time.Hour) {
		t.Fatal("Expected cookie to be expired but wasn't")
	}
}

func Test_JWTMux_Login_RedirectsToCorrectURL(t *testing.T) {
	t.Parallel()

	mp := &MockProvider{"biff@example.com"}
	jm := oauth2.NewJWTMux(mp, &YesManAuthenticator{}, clog.New(clog.ParseLevel("debug")))

	jm.Now = func() time.Time {
		return testTime
	}

	ts := httptest.NewServer(jm.Login())
	defer ts.Close()

	hc := http.Client{
		CheckRedirect: func(r *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}

	resp, err := hc.Get(ts.URL)
	if err != nil {
		t.Fatal("Error communicating with Login() handler: err:", err)
	}

	// Ensure we were redirected
	if resp.StatusCode < 300 || resp.StatusCode >= 400 {
		t.Fatal("Expected to be redirected, but received status code", resp.StatusCode)
	}

	loc, err := resp.Location()
	if err != nil {
		t.Fatal("Expected a location to be redirected to, but wasn't present")
	}

	if state := loc.Query().Get("state"); state != "HELLO?!MCFLY?!ANYONEINTHERE?!" {
		t.Fatal("Expected state to be set but was", state)
	}
}
<|endoftext|>


<commit_before>package sfv

import (
	"bufio"
	"fmt"
	"hash/crc32"
	"io"
	"os"
	"path"
	"strconv"
	"strings"
)

type Checksum struct {
	Filename string
	Path     string
	CRC32    uint32
}

type SFV struct {
	Checksums []Checksum
}

func (c *Checksum) Verify() (bool, error) {
	f, err := os.Open(c.Path)
	if err != nil {
		return false, err
	}
	defer f.Close()

	h := crc32.NewIEEE()
	reader := bufio.NewReader(f)
	buf := make([]byte, 4096)
	for {
		n, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			return false, err
		}
		if n == 0 {
			break
		}
		h.Write(buf[:n])
	}
	success := h.Sum32() == c.CRC32
	return success, nil
}

func (c *SFV) Verify() (bool, error) {
	for _, c := range c.Checksums {
		ok, err := c.Verify()
		if err != nil {
			return false, err
		}
		if !ok {
			return false, nil
		}
	}
	return true, nil
}

func parseChecksum(dir string, s string) (*Checksum, error) {
	words := strings.SplitN(s, " ", 2)
	if len(words) != 2 {
		return nil, fmt.Errorf("expected 2 words, got %d", len(words))
	}

	filename := words[0]
	path := path.Join(dir, filename)
	crcString := strings.TrimLeft(words[1], " \t")
	crc32, err := strconv.ParseUint(crcString, 16, 32)
	if err != nil {
		return nil, err
	}
	// ParseUint will return error if number exceeds 32 bits
	return &Checksum{
		Path:     path,
		Filename: filename,
		CRC32:    uint32(crc32),
	}, nil
}

func Read(filepath string) (*SFV, error) {
	f, err := os.Open(filepath)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	dir := path.Dir(filepath)
	checksums := []Checksum{}
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := strings.Trim(scanner.Text(), " \t")
		if len(line) == 0 || strings.HasPrefix(line, ";") {
			continue
		}
		checksum, err := parseChecksum(dir, line)
		if err != nil {
			return nil, err
		}
		checksums = append(checksums, *checksum)
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return &SFV{Checksums: checksums}, nil
}
<commit_msg>Add documentation<commit_after>// Package sfv provides a simple way of reading and verifying SFV (Simple File
// Verification) files.
package sfv

import (
	"bufio"
	"fmt"
	"hash/crc32"
	"io"
	"os"
	"path"
	"strconv"
	"strings"
)

// Checksum represents a line in a SFV file, containing the filename, full path
// to the file and the CRC32 checksum
type Checksum struct {
	Filename string
	Path     string
	CRC32    uint32
}

// SFV contains all the checksums read from a SFV file.
type SFV struct {
	Checksums []Checksum
}

// Verify calculates the CRC32 of the associated file and returns true if the
// checksum is correct.
func (c *Checksum) Verify() (bool, error) {
	f, err := os.Open(c.Path)
	if err != nil {
		return false, err
	}
	defer f.Close()

	h := crc32.NewIEEE()
	reader := bufio.NewReader(f)
	buf := make([]byte, 4096)
	for {
		n, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			return false, err
		}
		if n == 0 {
			break
		}
		h.Write(buf[:n])
	}
	success := h.Sum32() == c.CRC32
	return success, nil
}

// Verify verifies all checksums contained in SFV and returns true if all
// checksums are correct.
func (c *SFV) Verify() (bool, error) {
	for _, c := range c.Checksums {
		ok, err := c.Verify()
		if err != nil {
			return false, err
		}
		if !ok {
			return false, nil
		}
	}
	return true, nil
}

func parseChecksum(dir string, s string) (*Checksum, error) {
	words := strings.SplitN(s, " ", 2)
	if len(words) != 2 {
		return nil, fmt.Errorf("expected 2 words, got %d", len(words))
	}

	filename := words[0]
	path := path.Join(dir, filename)
	crcString := strings.TrimLeft(words[1], " \t")
	crc32, err := strconv.ParseUint(crcString, 16, 32)
	if err != nil {
		return nil, err
	}
	// ParseUint will return error if number exceeds 32 bits
	return &Checksum{
		Path:     path,
		Filename: filename,
		CRC32:    uint32(crc32),
	}, nil
}

// Read reades a SFV file from filepath and creates a new SFV containing
// checksums parsed from the SFV file.
func Read(filepath string) (*SFV, error) {
	f, err := os.Open(filepath)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	dir := path.Dir(filepath)
	checksums := []Checksum{}
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		line := strings.Trim(scanner.Text(), " \t")
		if len(line) == 0 || strings.HasPrefix(line, ";") {
			continue
		}
		checksum, err := parseChecksum(dir, line)
		if err != nil {
			return nil, err
		}
		checksums = append(checksums, *checksum)
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return &SFV{Checksums: checksums}, nil
}
<|endoftext|>


<commit_before>package yelp

import (
	"testing"
)

/**
 * Check using location options with bounding coordinates
 */
func TestLocationOptions(t *testing.T) {
	client := getClient(t)
	options := SearchOptions{
		GeneralOptions: &GeneralOptions{
			Term: "coffee",
		},
		LocationOptions: &LocationOptions{
			Location: "seattle",
		},
	}
	result, err := client.doSearch(options)
	check(t, err)
	assert(t, len(result.Businesses) > 0, CONTAINS_RESULTS)
}
<commit_msg>add location with coordinates test<commit_after>package yelp

import (
	"testing"

	"github.com/guregu/null"
)

/**
 * Check using location options by term
 */
func TestLocationOptions(t *testing.T) {
	client := getClient(t)
	options := SearchOptions{
		GeneralOptions: &GeneralOptions{
			Term: "coffee",
		},
		LocationOptions: &LocationOptions{
			Location: "seattle",
		},
	}
	result, err := client.doSearch(options)
	check(t, err)
	assert(t, len(result.Businesses) > 0, CONTAINS_RESULTS)
}

/**
 * Check using location options with bounding coordinates
 */
func TestLocationWithCoordinates(t *testing.T) {
	client := getClient(t)
	options := SearchOptions{
		GeneralOptions: &GeneralOptions{
			Term: "food",
		},
		LocationOptions: &LocationOptions{
			"bellevue",
			&CoordinateOptions{
				Latitude:  null.FloatFrom(37.788022),
				Longitude: null.FloatFrom(-122.399797),
			},
		},
	}
	result, err := client.doSearch(options)
	check(t, err)
	assert(t, len(result.Businesses) > 0, CONTAINS_RESULTS)
}
<|endoftext|>