<commit_before>package main

import (
	"fmt"
	"go/build"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

type dependencyRule struct {
	IsDenyRule        bool
	PackageExpression string
}

type dependencyPolicy struct {
	Incoming, Outgoing []dependencyRule
}

type packageConfig struct {
	Dependencies dependencyPolicy
	Path         string
}

type dependencyPolicyAction int

const (
	undecidedPolicyAction dependencyPolicyAction = iota
	approvedPolicyAction
	rejectedPolicyAction
)

type dependencyDirection int

const (
	incomingDependency dependencyDirection = iota
	outgoingDependency
)

type dependencyRuleReference struct {
	Package, MatchingPackage *build.Package
	InternalPackage          bool
	Path                     string
	Direction                dependencyDirection
	RuleIndex                int
	RuleSet                  []dependencyRule
}

type dependencyViolationError struct{}

func (*dependencyViolationError) Error() string {
	return "dependency policy violation"
}

func isDependencyViolation(a error) bool {
	_, ok := a.(*dependencyViolationError)
	return ok
}

func (r dependencyRule) enforce(p *build.Package) (dependencyPolicyAction, error) {
	if r.PackageExpression == "..." {
		if p.Goroot {
			return undecidedPolicyAction, nil
		}
		if r.IsDenyRule {
			return rejectedPolicyAction, nil
		}
		return approvedPolicyAction, nil
	}

	re := regexp.QuoteMeta(r.PackageExpression)
	if strings.HasSuffix(re, `/\.\.\.`) {
		re = re[:len(re)-len(`/\.\.\.`)] + `(/.*)?`
	}

	if matched, err := regexp.MatchString("^"+re+"$", p.ImportPath); err != nil {
		return undecidedPolicyAction, err
	} else if matched {
		if r.IsDenyRule {
			return rejectedPolicyAction, nil
		}
		return approvedPolicyAction, nil
	}
	return undecidedPolicyAction, nil
}

func collectDirs(root, suffix string, dirs map[string]bool) error {
	path := filepath.Join(root, suffix)
	fis, err := ioutil.ReadDir(path)
	if err != nil {
		return fmt.Errorf("ReadDir(%v) failed: %v", path, err)
	}
	for _, fi := range fis {
		if fi.IsDir() {
			suffix2 := filepath.Join(suffix, fi.Name())
			dirs[suffix2] = true
			collectDirs(root, suffix2, dirs)
		}
	}
	return nil
}

func computeIncomingDependencies() (map[string]map[string]bool, error) {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		return nil, fmt.Errorf("GOPATH is not set")
	}
	dirs := strings.Split(gopath, ":")
	allDirs := map[string]bool{}
	for _, dir := range dirs {
		if err := collectDirs(filepath.Join(dir, "src"), "", allDirs); err != nil {
			return nil, err
		}
	}
	allDeps := map[string]map[string]bool{}
	for dir, _ := range allDirs {
		allDeps[dir] = map[string]bool{}
	}
	for dir, _ := range allDirs {
		mode := build.ImportMode(0)
		pkg, err := build.Import(dir, "", mode)
		if err != nil {
			fmt.Errorf("Import(%v, %v) failed: %v", dir, mode, err)
		}
		imports := pkg.Imports
		if includeTestsFlag {
			imports = append(imports, pkg.TestImports...)
		}
		for _, dep := range imports {
			if deps, ok := allDeps[dep]; ok {
				deps[dir] = true
			}
		}
	}
	return allDeps, nil
}

func enforceDependencyRulesOnPackage(rules []dependencyRule, p *build.Package) (dependencyPolicyAction, int, error) {
	for i, r := range rules {
		if x, err := r.enforce(p); err != nil {
			return x, i, err
		} else if x != undecidedPolicyAction {
			return x, i, nil
		}
	}
	return undecidedPolicyAction, -1, nil
}

func validateDependencyRelationship(p, x *build.Package, direction dependencyDirection) (dependencyRuleReference, error) {
	it := newPackageConfigFileIterator(p)

	for it.Advance() {
		c := it.Value()
		ruleSet := c.Dependencies.Outgoing
		if direction == incomingDependency {
			ruleSet = c.Dependencies.Incoming
		}

		action, index, err := enforceDependencyRulesOnPackage(ruleSet, x)
		ref := dependencyRuleReference{
			Package:         p,
			MatchingPackage: x,
			Path:            c.Path,
			Direction:       direction,
			RuleIndex:       index,
			RuleSet:         ruleSet,
		}

		if err != nil {
			return ref, err
		}

		switch action {
		case approvedPolicyAction:
			return ref, nil
		case rejectedPolicyAction:
			return ref, &dependencyViolationError{}
		}

		if direction == incomingDependency {
			pkgConfDir := filepath.Dir(c.Path)
			pkgName := filepath.Base(pkgConfDir)
			if pkgName == "internal" {
				internalPackagePrefix := filepath.Dir(pkgConfDir)
				if internalPackagePrefix != x.Dir && !strings.HasPrefix(x.Dir, internalPackagePrefix+"/") {
					return dependencyRuleReference{
						Package:         p,
						MatchingPackage: x,
						Path:            c.Path,
						Direction:       direction,
						InternalPackage: true,
					}, &dependencyViolationError{}
				}
			}
		}
	}

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

	return dependencyRuleReference{}, nil
}

func printDependencyHierarchy(stdout io.Writer, p *build.Package, visited map[*build.Package]bool, depth int) error {
	if prettyFlag {
		for i := 0; i < depth-1; i++ {
			fmt.Fprintf(stdout, " │")
		}
		if depth > 0 {
			fmt.Fprintf(stdout, " ├─")
		} else {
			fmt.Fprintf(stdout, "#")
		}
		fmt.Fprintln(stdout, p.ImportPath)
	} else {
		if depth > 0 {
			fmt.Fprintln(stdout, p.ImportPath)
		}
	}

	if visited[p] || (!transitiveFlag && depth == 1) {
		return nil
	}

	visited[p] = true
	imports := p.Imports
	if includeTestsFlag {
		imports = append(imports, p.TestImports...)
	}
	for _, dep := range imports {
		pkg, err := importPackage(dep)
		if err != nil {
			return err
		}
		if gorootFlag || !pkg.Goroot {
			if err := printDependencyHierarchy(stdout, pkg, visited, depth+1); err != nil {
				return err
			}
		}
	}

	return nil
}

func verifyDependencyHierarchy(p *build.Package, visited map[*build.Package]bool, parent *build.Package, recurse bool) ([]dependencyRuleReference, error) {
	v := []dependencyRuleReference{}

	if parent != nil {
		r, err := validateDependencyRelationship(parent, p, outgoingDependency)
		if err != nil {
			if isDependencyViolation(err) {
				v = append(v, r)
			} else {
				return v, err
			}
		}
		r, err = validateDependencyRelationship(p, parent, incomingDependency)
		if err != nil {
			if isDependencyViolation(err) {
				v = append(v, r)
			} else {
				return v, err
			}
		}
	}

	if visited[p] {
		return nil, nil
	}
	visited[p] = true
	if parent == nil || recurse {
		imports := p.Imports
		if includeTestsFlag {
			imports = append(imports, p.TestImports...)
		}
		for _, importPath := range imports {
			dependency, err := importPackage(importPath)
			if err == nil {
				var depViolation []dependencyRuleReference
				depViolation, err = verifyDependencyHierarchy(dependency, visited, p, recurse)
				if depViolation != nil {
					v = append(v, depViolation...)
				}
			}
			if err != nil {
				return v, err
			}
		}
	}
	return v, nil
}
<commit_msg>Revert "veyron.io/tools: fix flaky test"<commit_after>package main

import (
	"fmt"
	"go/build"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

type dependencyRule struct {
	IsDenyRule        bool
	PackageExpression string
}

type dependencyPolicy struct {
	Incoming, Outgoing []dependencyRule
}

type packageConfig struct {
	Dependencies dependencyPolicy
	Path         string
}

type dependencyPolicyAction int

const (
	undecidedPolicyAction dependencyPolicyAction = iota
	approvedPolicyAction
	rejectedPolicyAction
)

type dependencyDirection int

const (
	incomingDependency dependencyDirection = iota
	outgoingDependency
)

type dependencyRuleReference struct {
	Package, MatchingPackage *build.Package
	InternalPackage          bool
	Path                     string
	Direction                dependencyDirection
	RuleIndex                int
	RuleSet                  []dependencyRule
}

type dependencyViolationError struct{}

func (*dependencyViolationError) Error() string {
	return "dependency policy violation"
}

func isDependencyViolation(a error) bool {
	_, ok := a.(*dependencyViolationError)
	return ok
}

func (r dependencyRule) enforce(p *build.Package) (dependencyPolicyAction, error) {
	if r.PackageExpression == "..." {
		if p.Goroot {
			return undecidedPolicyAction, nil
		}
		if r.IsDenyRule {
			return rejectedPolicyAction, nil
		}
		return approvedPolicyAction, nil
	}

	re := regexp.QuoteMeta(r.PackageExpression)
	if strings.HasSuffix(re, `/\.\.\.`) {
		re = re[:len(re)-len(`/\.\.\.`)] + `(/.*)?`
	}

	if matched, err := regexp.MatchString("^"+re+"$", p.ImportPath); err != nil {
		return undecidedPolicyAction, err
	} else if matched {
		if r.IsDenyRule {
			return rejectedPolicyAction, nil
		}
		return approvedPolicyAction, nil
	}
	return undecidedPolicyAction, nil
}

func collectDirs(root, suffix string, dirs map[string]struct{}) error {
	path := filepath.Join(root, suffix)
	fis, err := ioutil.ReadDir(path)
	if err != nil {
		return fmt.Errorf("ReadDir(%v) failed: %v", path, err)
	}
	for _, fi := range fis {
		if fi.IsDir() {
			suffix2 := filepath.Join(suffix, fi.Name())
			dirs[suffix2] = struct{}{}
			collectDirs(root, suffix2, dirs)
		}
	}
	return nil
}

func computeIncomingDependencies() (map[string]map[string]struct{}, error) {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		return nil, fmt.Errorf("GOPATH is not set")
	}
	dirs := strings.Split(gopath, ":")
	allDirs := map[string]struct{}{}
	for _, dir := range dirs {
		if err := collectDirs(filepath.Join(dir, "src"), "", allDirs); err != nil {
			return nil, err
		}
	}
	allDeps := map[string]map[string]struct{}{}
	for dir, _ := range allDirs {
		allDeps[dir] = map[string]struct{}{}
	}
	for dir, _ := range allDirs {
		mode := build.ImportMode(0)
		pkg, err := build.Import(dir, "", mode)
		if err != nil {
			fmt.Errorf("Import(%v, %v) failed: %v", dir, mode, err)
		}
		imports := pkg.Imports
		if includeTestsFlag {
			imports = append(imports, pkg.TestImports...)
		}
		for _, dep := range imports {
			if deps, ok := allDeps[dep]; ok {
				deps[dir] = struct{}{}
			}
		}
	}
	return allDeps, nil
}

func enforceDependencyRulesOnPackage(rules []dependencyRule, p *build.Package) (dependencyPolicyAction, int, error) {
	for i, r := range rules {
		if x, err := r.enforce(p); err != nil {
			return x, i, err
		} else if x != undecidedPolicyAction {
			return x, i, nil
		}
	}
	return undecidedPolicyAction, -1, nil
}

func validateDependencyRelationship(p, x *build.Package, direction dependencyDirection) (dependencyRuleReference, error) {
	it := newPackageConfigFileIterator(p)

	for it.Advance() {
		c := it.Value()
		ruleSet := c.Dependencies.Outgoing
		if direction == incomingDependency {
			ruleSet = c.Dependencies.Incoming
		}

		action, index, err := enforceDependencyRulesOnPackage(ruleSet, x)
		ref := dependencyRuleReference{
			Package:         p,
			MatchingPackage: x,
			Path:            c.Path,
			Direction:       direction,
			RuleIndex:       index,
			RuleSet:         ruleSet,
		}

		if err != nil {
			return ref, err
		}

		switch action {
		case approvedPolicyAction:
			return ref, nil
		case rejectedPolicyAction:
			return ref, &dependencyViolationError{}
		}

		if direction == incomingDependency {
			pkgConfDir := filepath.Dir(c.Path)
			pkgName := filepath.Base(pkgConfDir)
			if pkgName == "internal" {
				internalPackagePrefix := filepath.Dir(pkgConfDir)
				if internalPackagePrefix != x.Dir && !strings.HasPrefix(x.Dir, internalPackagePrefix+"/") {
					return dependencyRuleReference{
						Package:         p,
						MatchingPackage: x,
						Path:            c.Path,
						Direction:       direction,
						InternalPackage: true,
					}, &dependencyViolationError{}
				}
			}
		}
	}

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

	return dependencyRuleReference{}, nil
}

func printDependencyHierarchy(stdout io.Writer, p *build.Package, visited map[*build.Package]bool, depth int) error {
	if prettyFlag {
		for i := 0; i < depth-1; i++ {
			fmt.Fprintf(stdout, " │")
		}
		if depth > 0 {
			fmt.Fprintf(stdout, " ├─")
		} else {
			fmt.Fprintf(stdout, "#")
		}
		fmt.Fprintln(stdout, p.ImportPath)
	} else {
		if depth > 0 {
			fmt.Fprintln(stdout, p.ImportPath)
		}
	}

	if visited[p] || (!transitiveFlag && depth == 1) {
		return nil
	}

	visited[p] = true
	imports := p.Imports
	if includeTestsFlag {
		imports = append(imports, p.TestImports...)
	}
	for _, dep := range imports {
		pkg, err := importPackage(dep)
		if err != nil {
			return err
		}
		if gorootFlag || !pkg.Goroot {
			if err := printDependencyHierarchy(stdout, pkg, visited, depth+1); err != nil {
				return err
			}
		}
	}

	return nil
}

func verifyDependencyHierarchy(p *build.Package, visited map[*build.Package]bool, parent *build.Package, recurse bool) ([]dependencyRuleReference, error) {
	v := []dependencyRuleReference{}

	if parent != nil {
		r, err := validateDependencyRelationship(parent, p, outgoingDependency)
		if err != nil {
			if isDependencyViolation(err) {
				v = append(v, r)
			} else {
				return v, err
			}
		}
		r, err = validateDependencyRelationship(p, parent, incomingDependency)
		if err != nil {
			if isDependencyViolation(err) {
				v = append(v, r)
			} else {
				return v, err
			}
		}
	}

	if visited[p] {
		return nil, nil
	}
	visited[p] = true
	if parent == nil || recurse {
		imports := p.Imports
		if includeTestsFlag {
			imports = append(imports, p.TestImports...)
		}
		for _, importPath := range imports {
			dependency, err := importPackage(importPath)
			if err == nil {
				var depViolation []dependencyRuleReference
				depViolation, err = verifyDependencyHierarchy(dependency, visited, p, recurse)
				if depViolation != nil {
					v = append(v, depViolation...)
				}
			}
			if err != nil {
				return v, err
			}
		}
	}
	return v, nil
}
<|endoftext|>


<commit_before>package bio

import (
	"bytes"
	"io"
	"os"
	"testing"

	"github.com/chzyer/test"
)

var _ RawDisker = new(File)

func TestFileException(t *testing.T) {
	defer test.New(t)
	root := test.Root()

	_, err := NewFileEx(root, 33)
	test.NotNil(err)

	f, err := NewFileEx(root, 10)
	test.Nil(err)

	err = f.Delete(false)
	test.Nil(err)

	_, err = f.ReadAt(nil, -1)
	test.Equal(err, ErrFileInvalidOffset)
	test.Equal(f.Close(), nil)
	_, err = f.WriteAt(nil, 1)
	test.Equal(err, ErrFileClosed)
	_, err = f.ReadAt(nil, 1)
	test.Equal(err, ErrFileClosed)
}

func TestFileWriteRead(t *testing.T) {
	defer test.New(t)
	f, err := NewFileEx(test.Root(), 4)
	test.Nil(err)
	defer f.Close()
	f.Delete(false)

	buf := []byte("abcdefgh")

	for i := 0; i < 1024; i += 8 {
		n, err := f.WriteAt(buf, int64(i))
		test.Equals(n, 8, err, nil)
	}

	buf2 := make([]byte, len(buf))
	for i := 0; i < 1024; {
		n, err := f.ReadAt(buf2, int64(i))
		test.Equals(n, len(buf2), buf2, buf, err, nil)
		i += n
	}
}

func TestFile(t *testing.T) {
	defer test.New(t)

	f, err := NewFileEx(test.Root(), 2)
	test.Nil(err)
	test.Nil(f.Delete(false))
	defer f.Close()

	buf := bytes.Repeat([]byte("ha"), 1024)
	n, err := f.WriteAt(buf, 1)
	test.Nil(err)
	test.Equal(n, len(buf))

	buf2 := make([]byte, len(buf))
	n, err = f.ReadAt(buf2, 0)
	test.Nil(err)
	test.Equal(n, len(buf2))
	test.Equal(buf[:len(buf)-1], buf2[1:])

	n, err = f.ReadAt([]byte(" "), 1024*2+1)
	test.Equals(n, 0, err, io.EOF)

	n, err = f.ReadAt([]byte("  "), 0)
	test.Equals(n, 2, err, nil)

	n, err = f.ReadAt([]byte("  "), 0)
	test.Equals(n, 2, err, nil)

	os.RemoveAll(f.root)
	n, err = f.ReadAt([]byte("  "), 4)
	test.Equals(n, 0)
	test.Equal(err, io.EOF)

	n, err = f.WriteAt([]byte("  "), 4)
	test.Equals(n, 0)
	test.Equal(err, io.EOF)
}
<commit_msg>[bio] fix test<commit_after>package bio

import (
	"bytes"
	"io"
	"os"
	"testing"

	"github.com/chzyer/test"
)

var _ ReadWriterAt = new(File)

func TestFileException(t *testing.T) {
	defer test.New(t)
	root := test.Root()

	_, err := NewFileEx(root, 33)
	test.NotNil(err)

	f, err := NewFileEx(root, 10)
	test.Nil(err)

	err = f.Delete(false)
	test.Nil(err)

	_, err = f.ReadAt(nil, -1)
	test.Equal(err, ErrFileInvalidOffset)
	test.Equal(f.Close(), nil)
	_, err = f.WriteAt(nil, 1)
	test.Equal(err, ErrFileClosed)
	_, err = f.ReadAt(nil, 1)
	test.Equal(err, ErrFileClosed)
}

func TestFileWriteRead(t *testing.T) {
	defer test.New(t)
	f, err := NewFileEx(test.Root(), 4)
	test.Nil(err)
	defer f.Close()
	f.Delete(false)

	buf := []byte("abcdefgh")

	for i := 0; i < 1024; i += 8 {
		n, err := f.WriteAt(buf, int64(i))
		test.Equals(n, 8, err, nil)
	}

	buf2 := make([]byte, len(buf))
	for i := 0; i < 1024; {
		n, err := f.ReadAt(buf2, int64(i))
		test.Equals(n, len(buf2), buf2, buf, err, nil)
		i += n
	}
}

func TestFile(t *testing.T) {
	defer test.New(t)

	f, err := NewFileEx(test.Root(), 2)
	test.Nil(err)
	test.Nil(f.Delete(false))
	defer f.Close()

	buf := bytes.Repeat([]byte("ha"), 1024)
	n, err := f.WriteAt(buf, 1)
	test.Nil(err)
	test.Equal(n, len(buf))

	buf2 := make([]byte, len(buf))
	n, err = f.ReadAt(buf2, 0)
	test.Nil(err)
	test.Equal(n, len(buf2))
	test.Equal(buf[:len(buf)-1], buf2[1:])

	n, err = f.ReadAt([]byte(" "), 1024*2+1)
	test.Equals(n, 0, err, io.EOF)

	n, err = f.ReadAt([]byte("  "), 0)
	test.Equals(n, 2, err, nil)

	n, err = f.ReadAt([]byte("  "), 0)
	test.Equals(n, 2, err, nil)

	os.RemoveAll(f.root)
	n, err = f.ReadAt([]byte("  "), 4)
	test.Equals(n, 0)
	test.Equal(err, io.EOF)

	n, err = f.WriteAt([]byte("  "), 4)
	test.Equals(n, 0)
	test.Equal(err, io.EOF)
}
<|endoftext|>


<commit_before>package engine

import (
	"sync"
	"time"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/libkb/kex"
	keybase_1 "github.com/keybase/client/protocol/go"
)

// KexCom contains common functions for all kex engines.  It
// should be embedded in the kex engines.
type KexCom struct {
	server    kex.Handler
	user      *libkb.User
	deviceID  libkb.DeviceID
	debugName string
	wg        sync.WaitGroup
	rec       *kex.Receiver
	libkb.Contextified
}

func newKexCom(gc *libkb.GlobalContext) *KexCom {
	return &KexCom{Contextified: libkb.NewContextified(gc)}
}

func (k *KexCom) verifyReceiver(m *kex.Meta) error {
	k.G().Log.Debug("[%s] kex Meta: sender device %s => receiver device %s", k.debugName, m.Sender, m.Receiver)
	k.G().Log.Debug("[%s] kex Meta: own device %s", k.debugName, k.deviceID)
	if m.Receiver != k.deviceID {
		return libkb.ErrReceiverDevice
	}
	return nil
}

func (k *KexCom) verifyRequest(m *kex.Meta) error {
	if err := k.verifyReceiver(m); err != nil {
		return err
	}
	return nil
}

func (k *KexCom) poll(m *kex.Meta, secret *kex.Secret) {
	k.rec = kex.NewReceiver(m.Direction, secret)
	k.wg.Add(1)
	go func() {
		k.rec.Poll(m)
		k.wg.Done()
	}()
}

func (k *KexCom) next(name kex.MsgName, timeout time.Duration, handler func(*kex.Msg) error) error {
	k.G().Log.Debug("%s: waiting for %s (%s)", k.debugName, name, timeout)
	msg, err := k.rec.Next(name, timeout)
	k.G().Log.Debug("%s: got message %s", k.debugName, name)
	if err != nil {
		k.G().Log.Warning("%s: receiving Kex message %s gave error: %s", k.debugName, name, err.Error())
		return err
	}
	if err := k.verifyRequest(&msg.Meta); err != nil {
		k.G().Log.Warning("%s: verifying Kex message %s gave error: %s", k.debugName, name, err.Error())
		return err
	}
	k.G().Log.Debug("%s: dispatching message to handler: %s", k.debugName, name)
	return handler(msg)
}

func (k *KexCom) kexStatus(ctx *Context, msg string, code keybase_1.KexStatusCode) {

	if err := ctx.DoctorUI.KexStatus(keybase_1.KexStatusArg{Msg: msg, Code: code}); err != nil {
		// an error here isn't critical
		k.G().Log.Debug("send KexStatus error: %s", err)
	}
}

func (k *KexCom) cancel(m *kex.Meta) error {
	if err := k.rec.Cancel(); err != nil {
		return err
	}
	return k.server.Cancel(m)
}
<commit_msg>safety checks<commit_after>package engine

import (
	"sync"
	"time"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/libkb/kex"
	keybase_1 "github.com/keybase/client/protocol/go"
)

// KexCom contains common functions for all kex engines.  It
// should be embedded in the kex engines.
type KexCom struct {
	server    kex.Handler
	user      *libkb.User
	deviceID  libkb.DeviceID
	debugName string
	wg        sync.WaitGroup
	rec       *kex.Receiver
	libkb.Contextified
}

func newKexCom(gc *libkb.GlobalContext) *KexCom {
	return &KexCom{Contextified: libkb.NewContextified(gc)}
}

func (k *KexCom) verifyReceiver(m *kex.Meta) error {
	k.G().Log.Debug("[%s] kex Meta: sender device %s => receiver device %s", k.debugName, m.Sender, m.Receiver)
	k.G().Log.Debug("[%s] kex Meta: own device %s", k.debugName, k.deviceID)
	if m.Receiver != k.deviceID {
		return libkb.ErrReceiverDevice
	}
	return nil
}

func (k *KexCom) verifyRequest(m *kex.Meta) error {
	if err := k.verifyReceiver(m); err != nil {
		return err
	}
	return nil
}

func (k *KexCom) poll(m *kex.Meta, secret *kex.Secret) {
	k.rec = kex.NewReceiver(m.Direction, secret)
	k.wg.Add(1)
	go func() {
		k.rec.Poll(m)
		k.wg.Done()
	}()
}

func (k *KexCom) next(name kex.MsgName, timeout time.Duration, handler func(*kex.Msg) error) error {
	k.G().Log.Debug("%s: waiting for %s (%s)", k.debugName, name, timeout)
	msg, err := k.rec.Next(name, timeout)
	k.G().Log.Debug("%s: got message %s", k.debugName, name)
	if err != nil {
		k.G().Log.Warning("%s: receiving Kex message %s gave error: %s", k.debugName, name, err.Error())
		return err
	}
	if err := k.verifyRequest(&msg.Meta); err != nil {
		k.G().Log.Warning("%s: verifying Kex message %s gave error: %s", k.debugName, name, err.Error())
		return err
	}
	k.G().Log.Debug("%s: dispatching message to handler: %s", k.debugName, name)
	return handler(msg)
}

func (k *KexCom) kexStatus(ctx *Context, msg string, code keybase_1.KexStatusCode) {
	// just to be sure...
	if ctx.DoctorUI == nil {
		k.G().Log.Warning("KexCom kexStatus(), ctx.DoctorUI is nil")
		return
	}

	if err := ctx.DoctorUI.KexStatus(keybase_1.KexStatusArg{Msg: msg, Code: code}); err != nil {
		// an error here isn't critical
		k.G().Log.Debug("send KexStatus error: %s", err)
	}
}

func (k *KexCom) cancel(m *kex.Meta) error {
	if k.rec != nil {
		if err := k.rec.Cancel(); err != nil {
			return err
		}
	}
	if k.server != nil {
		if err := k.server.Cancel(m); err != nil {
			return err
		}
	}
	return nil
}
<|endoftext|>


<commit_before>// Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

// 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 master

import (
	"os"
	"time"

	"github.com/PaddlePaddle/Paddle/go/connection"
	"github.com/PaddlePaddle/recordio"
	"github.com/coreos/etcd/clientv3"
	log "github.com/sirupsen/logrus"
)

// Client is the client of the master server.
type Client struct {
	conn    *connection.Conn
	ch      chan record
	bufSize int
}

type record struct {
	r   []byte
	err error
}

// WithBuffer sets the client to buffer the training record.
//
// bufSize is the record buffer size. NextRecord will read from this
// buffer.
func WithBuffer(bufSize int) func(*Client) error {
	return func(c *Client) error {
		if bufSize <= 0 {
			return nil
		}
		c.bufSize = bufSize
		return nil
	}
}

// WithAddr sets the client to use fixed master address.
func WithAddr(addr string) func(c *Client) error {
	return func(c *Client) error {
		ch := make(chan string, 1)
		ch <- addr
		go c.monitorMaster(ch)
		return nil
	}
}

func retry(f func() error, dur time.Duration, count int) error {
	err := f()
	if err != nil {
		if count > 0 {
			time.Sleep(dur)
			return retry(f, dur, count-1)
		}
		return err
	}
	return nil
}

// WithEtcd sets the client to use etcd for master discovery.
func WithEtcd(endpoints []string, timeout time.Duration) func(*Client) error {
	return func(c *Client) error {
		var cli *clientv3.Client
		f := func() error {
			var err error
			cli, err = clientv3.New(clientv3.Config{
				Endpoints:   endpoints,
				DialTimeout: timeout,
			})
			return err
		}
		err := retry(f, time.Second, 10)
		if err != nil {
			return err
		}

		ch := make(chan string, 1)
		a, err := GetKey(cli, DefaultAddrPath, timeout)
		if err != nil {
			return err
		}

		if a != "" {
			// Master is registered, send to the master address
			// channel.
			ch <- a
		}

		go watchKey(cli, DefaultAddrPath, ch)
		go c.monitorMaster(ch)
		return nil
	}
}

// NewClient creates a new Client.
func NewClient(opts ...func(*Client) error) (*Client, error) {
	c := &Client{}
	c.conn = connection.New()

	for _, opt := range opts {
		err := opt(c)
		if err != nil {
			return nil, err
		}
	}
	c.ch = make(chan record, c.bufSize)
	return c, nil
}

// StartGetRecords must be called at beginning of each pass
func (c *Client) StartGetRecords(passID int) {
	go c.getRecords(passID)
}

func (c *Client) getRecords(passID int) {
	for {
		t, err := c.getTask(passID)
		if err != nil {
			if err.Error() == ErrPassBefore.Error() ||
				err.Error() == ErrNoMoreAvailable.Error() ||
				err.Error() == ErrAllTaskFailed.Error() {
				c.ch <- record{nil, err}
				break
			}
			if err.Error() == ErrPassAfter.Error() {
				// wait util last pass finishes
				time.Sleep(time.Second * 3)
				continue
			}
			log.Errorf("getTask error: %s", err)
		}

		for _, chunk := range t.Chunks {
			f, e := os.Open(chunk.Path)
			if e != nil {
				log.Errorln(e)
				continue
			}

			s := recordio.NewRangeScanner(f, &chunk.Index, -1, -1)
			for s.Scan() {
				c.ch <- record{s.Record(), nil}
			}

			if s.Err() != nil {
				c.ch <- record{nil, s.Err()}
				log.Errorln(err, chunk.Path)
			}

			err = f.Close()
			if err != nil {
				log.Errorln(err)
			}
		}

		// We treat a task as finished whenever the last data
		// instance of the task is read. This is not exactly
		// correct, but a reasonable approximation.
		err = c.taskFinished(t.Meta.ID)
		if err != nil {
			log.Errorln(err)
		}
	}
}

func (c *Client) monitorMaster(addrCh <-chan string) {
	lastMaster := ""
	for curMaster := range addrCh {
		// connect to the new address once address changed.
		if curMaster != lastMaster {
			if curMaster == "" {
				err := c.conn.Close()
				if err != nil {
					log.Errorln(err)
				}
			} else {
				err := c.conn.Connect(curMaster)
				if err != nil {
					log.Errorln(err)

					// connect to addr failed, set
					// to last known addr in order
					// to retry next time.
					curMaster = lastMaster
				}
			}
		}
		lastMaster = curMaster
	}
}

// SetDataset sets dataset to dispatch for the master server.
//
// SetDataset can be call multiple times at one pass. But only the first call
// will be honored.
//
// After all tasks are done, another call of SetDataset will start another pass.
func (c *Client) SetDataset(globPaths []string) error {
	err := c.conn.Call("Service.SetDataset", globPaths, nil)
	return err
}

// getTask gets a new task from the master server.
func (c *Client) getTask(passID int) (Task, error) {
	var t Task
	err := c.conn.Call("Service.GetTask", passID, &t)
	return t, err
}

// TaskFinished tells the master server a task is finished.
func (c *Client) taskFinished(taskID int) error {
	return c.conn.Call("Service.TaskFinished", taskID, nil)
}

// TaskFailed tell the master server as task is failed.
func (c *Client) taskFailed(meta TaskMeta) error {
	return c.conn.Call("Service.TaskFailed", meta, nil)
}

// NextRecord returns next record in the dataset.
//
// NextRecord will block until the next record is available. It is
// thread-safe.
func (c *Client) NextRecord() ([]byte, error) {
	r := <-c.ch
	return r.r, r.err
}

// RequestSaveModel requests the master server to approve the caller
// to save the model.
func (c *Client) RequestSaveModel(trainerID string, blockDur time.Duration) (bool, error) {
	var need bool
	err := c.conn.Call("Service.RequestSaveModel", SaveModelRequest{TrainerID: trainerID, BlockDur: blockDur}, &need)
	return need, err
}
<commit_msg>master server will wait etcd forever<commit_after>// Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

// 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 master

import (
	"os"
	"time"

	"github.com/PaddlePaddle/Paddle/go/connection"
	"github.com/PaddlePaddle/recordio"
	"github.com/coreos/etcd/clientv3"
	log "github.com/sirupsen/logrus"
)

// Client is the client of the master server.
type Client struct {
	conn    *connection.Conn
	ch      chan record
	bufSize int
}

type record struct {
	r   []byte
	err error
}

// WithBuffer sets the client to buffer the training record.
//
// bufSize is the record buffer size. NextRecord will read from this
// buffer.
func WithBuffer(bufSize int) func(*Client) error {
	return func(c *Client) error {
		if bufSize <= 0 {
			return nil
		}
		c.bufSize = bufSize
		return nil
	}
}

// WithAddr sets the client to use fixed master address.
func WithAddr(addr string) func(c *Client) error {
	return func(c *Client) error {
		ch := make(chan string, 1)
		ch <- addr
		go c.monitorMaster(ch)
		return nil
	}
}

// WithEtcd sets the client to use etcd for master discovery.
func WithEtcd(endpoints []string, timeout time.Duration) func(*Client) error {
	return func(c *Client) error {
		var cli *clientv3.Client
		f := func() error {
			var err error
			cli, err = clientv3.New(clientv3.Config{
				Endpoints:   endpoints,
				DialTimeout: timeout,
			})
			return err
		}
		for {
			err := f()
			if err != nil {
				log.Warningln(err)
			} else {
				break
			}
			time.Sleep(time.Second)
		}

		ch := make(chan string, 1)
		a, err := GetKey(cli, DefaultAddrPath, timeout)
		if err != nil {
			return err
		}

		if a != "" {
			// Master is registered, send to the master address
			// channel.
			ch <- a
		}

		go watchKey(cli, DefaultAddrPath, ch)
		go c.monitorMaster(ch)
		return nil
	}
}

// NewClient creates a new Client.
func NewClient(opts ...func(*Client) error) (*Client, error) {
	c := &Client{}
	c.conn = connection.New()

	for _, opt := range opts {
		err := opt(c)
		if err != nil {
			return nil, err
		}
	}
	c.ch = make(chan record, c.bufSize)
	return c, nil
}

// StartGetRecords must be called at beginning of each pass
func (c *Client) StartGetRecords(passID int) {
	go c.getRecords(passID)
}

func (c *Client) getRecords(passID int) {
	for {
		t, err := c.getTask(passID)
		if err != nil {
			if err.Error() == ErrPassBefore.Error() ||
				err.Error() == ErrNoMoreAvailable.Error() ||
				err.Error() == ErrAllTaskFailed.Error() {
				c.ch <- record{nil, err}
				break
			}
			if err.Error() == ErrPassAfter.Error() {
				// wait util last pass finishes
				time.Sleep(time.Second * 3)
				continue
			}
			log.Errorf("getTask error: %s", err)
		}

		for _, chunk := range t.Chunks {
			f, e := os.Open(chunk.Path)
			if e != nil {
				log.Errorln(e)
				continue
			}

			s := recordio.NewRangeScanner(f, &chunk.Index, -1, -1)
			for s.Scan() {
				c.ch <- record{s.Record(), nil}
			}

			if s.Err() != nil {
				c.ch <- record{nil, s.Err()}
				log.Errorln(err, chunk.Path)
			}

			err = f.Close()
			if err != nil {
				log.Errorln(err)
			}
		}

		// We treat a task as finished whenever the last data
		// instance of the task is read. This is not exactly
		// correct, but a reasonable approximation.
		err = c.taskFinished(t.Meta.ID)
		if err != nil {
			log.Errorln(err)
		}
	}
}

func (c *Client) monitorMaster(addrCh <-chan string) {
	lastMaster := ""
	for curMaster := range addrCh {
		// connect to the new address once address changed.
		if curMaster != lastMaster {
			if curMaster == "" {
				err := c.conn.Close()
				if err != nil {
					log.Errorln(err)
				}
			} else {
				err := c.conn.Connect(curMaster)
				if err != nil {
					log.Errorln(err)

					// connect to addr failed, set
					// to last known addr in order
					// to retry next time.
					curMaster = lastMaster
				}
			}
		}
		lastMaster = curMaster
	}
}

// SetDataset sets dataset to dispatch for the master server.
//
// SetDataset can be call multiple times at one pass. But only the first call
// will be honored.
//
// After all tasks are done, another call of SetDataset will start another pass.
func (c *Client) SetDataset(globPaths []string) error {
	err := c.conn.Call("Service.SetDataset", globPaths, nil)
	return err
}

// getTask gets a new task from the master server.
func (c *Client) getTask(passID int) (Task, error) {
	var t Task
	err := c.conn.Call("Service.GetTask", passID, &t)
	return t, err
}

// TaskFinished tells the master server a task is finished.
func (c *Client) taskFinished(taskID int) error {
	return c.conn.Call("Service.TaskFinished", taskID, nil)
}

// TaskFailed tell the master server as task is failed.
func (c *Client) taskFailed(meta TaskMeta) error {
	return c.conn.Call("Service.TaskFailed", meta, nil)
}

// NextRecord returns next record in the dataset.
//
// NextRecord will block until the next record is available. It is
// thread-safe.
func (c *Client) NextRecord() ([]byte, error) {
	r := <-c.ch
	return r.r, r.err
}

// RequestSaveModel requests the master server to approve the caller
// to save the model.
func (c *Client) RequestSaveModel(trainerID string, blockDur time.Duration) (bool, error) {
	var need bool
	err := c.conn.Call("Service.RequestSaveModel", SaveModelRequest{TrainerID: trainerID, BlockDur: blockDur}, &need)
	return need, err
}
<|endoftext|>


<commit_before>package main

import "gobject/gtk-3.0"
import (
	"./button_boxes"
	"./links"
	"./list_store"
	"./spinner"
	"./expander"
	"./color_selector"
	"./info_bar"
	"./entry_buffer"
	"./pickers"
	"./main_window"
	"./paned_widgets"
)

type DemoFunc func(mainwin *gtk.Window) *gtk.Window

type DemoDesc struct {
	Title string
	Filename string
	Func DemoFunc
	Children []*DemoDesc
}

var demos = []*DemoDesc{
	{Title: "Application main window", Filename: "main_window.go",    Func: main_window.Do},
	{Title: "Button Boxes",            Filename: "button_boxes.go",   Func: button_boxes.Do},
	{Title: "Links",                   Filename: "links.go",          Func: links.Do},
	{Title: "Spinner",                 Filename: "spinner.go",        Func: spinner.Do},
	{Title: "Expander",                Filename: "expander.go",       Func: expander.Do},
	{Title: "Tree View", Children: []*DemoDesc{
		{Title: "List Store", Filename: "list_store.go", Func: list_store.Do},
	}},
	{Title: "Color Selector",          Filename: "color_selector.go", Func: color_selector.Do},
	{Title: "Info bar",                Filename: "info_bar.go",       Func: info_bar.Do},
	{Title: "Entry", Children: []*DemoDesc{
		{Title: "Entry Buffer", Filename: "entry_buffer.go", Func: entry_buffer.Do},
	}},
	{Title: "Pickers",                 Filename: "pickers.go",        Func: pickers.Do},
	{Title: "Paned Widgets",           Filename: "paned_widgets.go",  Func: paned_widgets.Do},
}
<commit_msg>Sort demos.<commit_after>package main

import "gobject/gtk-3.0"
import (
	"./button_boxes"
	"./links"
	"./list_store"
	"./spinner"
	"./expander"
	"./color_selector"
	"./info_bar"
	"./entry_buffer"
	"./pickers"
	"./main_window"
	"./paned_widgets"
)

type DemoFunc func(mainwin *gtk.Window) *gtk.Window

type DemoDesc struct {
	Title string
	Filename string
	Func DemoFunc
	Children []*DemoDesc
}

var demos = []*DemoDesc{
	{Title: "Application main window", Filename: "main_window.go",    Func: main_window.Do},
	{Title: "Button Boxes",            Filename: "button_boxes.go",   Func: button_boxes.Do},
	{Title: "Color Selector",          Filename: "color_selector.go", Func: color_selector.Do},
	{Title: "Entry", Children: []*DemoDesc{
		{Title: "Entry Buffer", Filename: "entry_buffer.go", Func: entry_buffer.Do},
	}},
	{Title: "Expander",                Filename: "expander.go",       Func: expander.Do},
	{Title: "Info bar",                Filename: "info_bar.go",       Func: info_bar.Do},
	{Title: "Links",                   Filename: "links.go",          Func: links.Do},
	{Title: "Paned Widgets",           Filename: "paned_widgets.go",  Func: paned_widgets.Do},
	{Title: "Pickers",                 Filename: "pickers.go",        Func: pickers.Do},
	{Title: "Spinner",                 Filename: "spinner.go",        Func: spinner.Do},
	{Title: "Tree View", Children: []*DemoDesc{
		{Title: "List Store", Filename: "list_store.go", Func: list_store.Do},
	}},
}
<|endoftext|>


<commit_before>/*
Copyright (c) 2014 VMware, Inc. All Rights Reserved.

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 device

import (
	"flag"
	"fmt"
	"os"
	"strings"
	"text/tabwriter"

	"github.com/vmware/govmomi/govc/cli"
	"github.com/vmware/govmomi/govc/flags"
	"github.com/vmware/govmomi/vim25/types"
)

type info struct {
	*flags.VirtualMachineFlag
}

func init() {
	cli.Register("device.info", &info{})
}

func (cmd *info) Register(f *flag.FlagSet) {}

func (cmd *info) Process() error { return nil }

func (cmd *info) Run(f *flag.FlagSet) error {
	vm, err := cmd.VirtualMachine()
	if err != nil {
		return err
	}

	if vm == nil {
		return flag.ErrHelp
	}

	devices, err := vm.Device()
	if err != nil {
		return err
	}

	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)

	for _, name := range f.Args() {
		device := devices.Find(name)
		if device == nil {
			return fmt.Errorf("device '%s' not found", name)
		}

		d := device.GetVirtualDevice()
		info := d.DeviceInfo.GetDescription()

		fmt.Fprintf(tw, "Name:\t%s\n", name)
		fmt.Fprintf(tw, "  Type:\t%s\n", devices.TypeName(device))
		fmt.Fprintf(tw, "  Label:\t%s\n", info.Label)
		fmt.Fprintf(tw, "  Summary:\t%s\n", info.Summary)
		fmt.Fprintf(tw, "  Key:\t%d\n", d.Key)

		if c, ok := device.(types.BaseVirtualController); ok {
			var attached []string
			for _, key := range c.GetVirtualController().Device {
				attached = append(attached, devices.Name(devices.FindByKey(key)))
			}
			fmt.Fprintf(tw, "  Devices:\t%s\n", strings.Join(attached, ", "))
		} else {
			if c := devices.FindByKey(d.ControllerKey); c != nil {
				fmt.Fprintf(tw, "  Controller:\t%s\n", devices.Name(c))
				fmt.Fprintf(tw, "  Unit number:\t%d\n", d.UnitNumber)
			}
		}

		if ca := d.Connectable; ca != nil {
			fmt.Fprintf(tw, "  Connected:\t%t\n", ca.Connected)
			fmt.Fprintf(tw, "  Start connected:\t%t\n", ca.StartConnected)
			fmt.Fprintf(tw, "  Guest control:\t%t\n", ca.AllowGuestControl)
			fmt.Fprintf(tw, "  Status:\t%s\n", ca.Status)
		}

		if net, ok := device.(types.BaseVirtualEthernetCard); ok {
			fmt.Fprintf(tw, "  MAC Address:\t%s\n", net.GetVirtualEthernetCard().MacAddress)
		}
	}

	return tw.Flush()
}
<commit_msg>Output disk file backing in device.info<commit_after>/*
Copyright (c) 2014 VMware, Inc. All Rights Reserved.

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 device

import (
	"flag"
	"fmt"
	"os"
	"strings"
	"text/tabwriter"

	"github.com/vmware/govmomi/govc/cli"
	"github.com/vmware/govmomi/govc/flags"
	"github.com/vmware/govmomi/vim25/types"
)

type info struct {
	*flags.VirtualMachineFlag
}

func init() {
	cli.Register("device.info", &info{})
}

func (cmd *info) Register(f *flag.FlagSet) {}

func (cmd *info) Process() error { return nil }

func (cmd *info) Run(f *flag.FlagSet) error {
	vm, err := cmd.VirtualMachine()
	if err != nil {
		return err
	}

	if vm == nil {
		return flag.ErrHelp
	}

	devices, err := vm.Device()
	if err != nil {
		return err
	}

	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)

	for _, name := range f.Args() {
		device := devices.Find(name)
		if device == nil {
			return fmt.Errorf("device '%s' not found", name)
		}

		d := device.GetVirtualDevice()
		info := d.DeviceInfo.GetDescription()

		fmt.Fprintf(tw, "Name:\t%s\n", name)
		fmt.Fprintf(tw, "  Type:\t%s\n", devices.TypeName(device))
		fmt.Fprintf(tw, "  Label:\t%s\n", info.Label)
		fmt.Fprintf(tw, "  Summary:\t%s\n", info.Summary)
		fmt.Fprintf(tw, "  Key:\t%d\n", d.Key)

		if c, ok := device.(types.BaseVirtualController); ok {
			var attached []string
			for _, key := range c.GetVirtualController().Device {
				attached = append(attached, devices.Name(devices.FindByKey(key)))
			}
			fmt.Fprintf(tw, "  Devices:\t%s\n", strings.Join(attached, ", "))
		} else {
			if c := devices.FindByKey(d.ControllerKey); c != nil {
				fmt.Fprintf(tw, "  Controller:\t%s\n", devices.Name(c))
				fmt.Fprintf(tw, "  Unit number:\t%d\n", d.UnitNumber)
			}
		}

		if ca := d.Connectable; ca != nil {
			fmt.Fprintf(tw, "  Connected:\t%t\n", ca.Connected)
			fmt.Fprintf(tw, "  Start connected:\t%t\n", ca.StartConnected)
			fmt.Fprintf(tw, "  Guest control:\t%t\n", ca.AllowGuestControl)
			fmt.Fprintf(tw, "  Status:\t%s\n", ca.Status)
		}

		switch md := device.(type) {
		case types.BaseVirtualEthernetCard:
			fmt.Fprintf(tw, "  MAC Address:\t%s\n", md.GetVirtualEthernetCard().MacAddress)
		case *types.VirtualDisk:
			if b, ok := md.Backing.(types.BaseVirtualDeviceFileBackingInfo); ok {
				fmt.Fprintf(tw, "  File:\t%s\n", b.GetVirtualDeviceFileBackingInfo().FileName)
			}
			if b, ok := md.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok && b.Parent != nil {
				fmt.Fprintf(tw, "  Parent:\t%s\n", b.Parent.GetVirtualDeviceFileBackingInfo().FileName)
			}
		}
	}

	return tw.Flush()
}
<|endoftext|>


<commit_before>package main

import (
	"strings"
	"fmt"
)

// Build uninstall script
func (i *Installation) createUninstallScript() error {
	// Uninstall script location
	uninstallFile := Config.INSTALL.UNINTSALLDIR + "uninstall_" + cmdOptions.Version + "_" + i.Timestamp
	Infof("Creating Uninstall file for this installation at: " + uninstallFile)

	// Query
	queryString := `
select $$ssh $$ || hostname || $$ "ps -ef|grep postgres|grep -v grep|grep $$ ||  port || $$ | awk '{print $2}'| xargs -n1 /bin/kill -11 &>/dev/null" $$ from gp_segment_configuration
union
select $$ssh $$ || hostname || $$ "rm -rf /tmp/.s.PGSQL.$$ || port || $$*"$$ from gp_segment_configuration
union
select $$ssh $$ || c.hostname || $$ "rm -rf $$ || f.fselocation || $$"$$ from pg_filespace_entry f, gp_segment_configuration c where c.dbid = f.fsedbid
`

	// Execute the query
	cmdOut, err := executeOsCommandOutput("psql", "-p", i.GPInitSystem.MasterPort, "-d", "template1", "-Atc", queryString)
	if err != nil {
		Fatalf("Error in running uninstall command on database, err: %v", err)
	}

	// Create the file
	createFile(uninstallFile)
	writeFile(uninstallFile, []string{
		string(cmdOut),
		"rm -rf "+ Config.INSTALL.ENVDIR +"env_" + cmdOptions.Version + "_"+ i.Timestamp,
		"rm -rf " + uninstallFile,

	})
	return nil
}

// Uninstall gpcc
func (i *Installation) uninstallGPCCScript() error {
	i.GPCC.UninstallFile = Config.INSTALL.UNINTSALLDIR + fmt.Sprintf("uninstall_gpcc_%s_%s_%s", cmdOptions.Version, cmdOptions.CCVersion, i.Timestamp)
	Infof("Created uninstall script for this version of GPCC Installation: %s", i.GPCC.UninstallFile)
	writeFile(i.GPCC.UninstallFile, []string{
		"source " + i.EnvFile,
		"source " + i.GPCC.GpPerfmonHome + "/gpcc_path.sh",
		"gpcmdr --stop " + i.GPCC.InstanceName + " &>/dev/null",
		"gpcc stop " + i.GPCC.InstanceName + " &>/dev/null",
		"rm -rf " + i.GPCC.GpPerfmonHome + "/instances/" + i.GPCC.InstanceName,
		"gpconfig -c gp_enable_gpperfmon -v off &>/dev/null",
		"echo \"Stopping the database to cleanup any gpperfmon process\"",
		"gpstop -af &>/dev/null",
		"echo \"Starting the database to remove any gpperfmon process\"",
		"gpstart -a &>/dev/null",
		"cp $MASTER_DATA_DIRECTORY/pg_hba.conf $MASTER_DATA_DIRECTORY/pg_hba.conf." + i.Timestamp + " &>/dev/null",
		"grep -v gpmon $MASTER_DATA_DIRECTORY/pg_hba.conf." + i.Timestamp + " > $MASTER_DATA_DIRECTORY/pg_hba.conf &>/dev/null",
		"rm -rf $MASTER_DATA_DIRECTORY/pg_hba.conf." + i.Timestamp + " &>/dev/null",
		"psql -d template1 -p $PGPORT -Atc \"drop database gpperfmon\" &>/dev/null",
		"psql -d template1 -p $PGPORT -Atc \"drop role gpmon\" &>/dev/null",
		"rm -rf $MASTER_DATA_DIRECTORY/gpperfmon/*",
		"cp " + i.EnvFile + " " + i.EnvFile + "." + i.Timestamp,
		"egrep -v \"GPCC_UNINSTALL_LOC|GPCCVersion|GPPERFMONHOME|GPCC_INSTANCE_NAME|GPCCPORT\" " + i.EnvFile + "." + i.Timestamp +" > " + i.EnvFile,
		"rm -rf " + i.EnvFile + "." + i.Timestamp,
		"rm -rf " + i.GPCC.UninstallFile,
	})
	return nil
}

// Uninstall using gpdeletesystem
func removeEnvGpDeleteSystem(envFile string) error {
	Info("Starting the database if stopped to run the gpdeletesystem on the environment")

	// Start the database if not started
	startDBifNotStarted(envFile)

	Infof("Calling gpdeletesystem to remove the environment: %s", envFile)

	// Write it to the file.
	file := Config.CORE.TEMPDIR + "run_deletesystem.sh"
	createFile(file)
	writeFile(file, []string{
		"source " + envFile,
		"gpdeletesystem -d $MASTER_DATA_DIRECTORY -f << EOF",
		"y",
		"y",
		"EOF",
	})
	_, err := executeOsCommandOutput("/bin/sh", file)
	if err != nil {
		deleteFile(file)
		return err
	}
	deleteFile(file)
	return nil
}

// Uninstall GPCC
func removeGPCC(envFile string) {
	Infof("Uninstalling the version of command center that is currently installed on this environment.")
	executeOsCommand("/bin/sh", environment(envFile).GpccUninstallLoc)
}

// Uninstall using manual method
func removeEnvManually(version, timestamp string) {
	uninstallScript := Config.INSTALL.UNINTSALLDIR + fmt.Sprintf("uninstall_%s_%s", version, timestamp)
	Infof("Cleaning up the extra files using the uninstall script: %s", uninstallScript)
	exists, err := doesFileOrDirExists(uninstallScript)
	if err != nil {
		Fatalf("error when trying to find the uninstaller file \"%s\", err: %v", uninstallScript, err)
	}
	if exists {
		executeOsCommandOutput("/bin/sh", uninstallScript)
	} else {
		Fatalf("Unable to find the uninstaller file \"%s\"", uninstallScript)
	}
}

// Main Remove method
func remove() {
	Infof("Starting program to uninstall the version: %s", cmdOptions.Version)

	// Check if the envfile for that version exists
	chosenEnvFile := installedEnvFiles(fmt.Sprintf("*%s*", cmdOptions.Version), "choose", true)

	// If we receive none, then display the error to user
	var timestamp, version string
	if IsValueEmpty(chosenEnvFile) {
		Fatalf("Cannot find any environment with the version: %s", cmdOptions.Version)
	} else { // Else store the value
		timestamp = strings.Split(chosenEnvFile, "_")[2]
		version = strings.Split(chosenEnvFile, "_")[1]
	}
	Infof("The choosen enviornment file to remove is: %s ", chosenEnvFile)
	Info("Uninstalling the environment")

	// If there is failure in gpstart, user can use force to force manual uninstallation
	if !cmdOptions.Force {
		err := removeEnvGpDeleteSystem(chosenEnvFile)
		if err != nil {
			Warnf("Failed to uninstall using gpdeletesystem, trying manual method..")
		}
	} else {
		Infof("Forcing uninstall of the environment: %s", chosenEnvFile)
	}

	// Uninstall GPPC
	removeGPCC(chosenEnvFile)

	// Run this to cleanup the file created by go-gpdb
	removeEnvManually(version, timestamp)

	Infof("Uninstallation of environment \"%s\" was a success", chosenEnvFile)
	Info("exiting ....")
}<commit_msg>Fixing a simple bug with redirect<commit_after>package main

import (
	"strings"
	"fmt"
)

// Build uninstall script
func (i *Installation) createUninstallScript() error {
	// Uninstall script location
	uninstallFile := Config.INSTALL.UNINTSALLDIR + "uninstall_" + cmdOptions.Version + "_" + i.Timestamp
	Infof("Creating Uninstall file for this installation at: " + uninstallFile)

	// Query
	queryString := `
select $$ssh $$ || hostname || $$ "ps -ef|grep postgres|grep -v grep|grep $$ ||  port || $$ | awk '{print $2}'| xargs -n1 /bin/kill -11 &>/dev/null" $$ from gp_segment_configuration
union
select $$ssh $$ || hostname || $$ "rm -rf /tmp/.s.PGSQL.$$ || port || $$*"$$ from gp_segment_configuration
union
select $$ssh $$ || c.hostname || $$ "rm -rf $$ || f.fselocation || $$"$$ from pg_filespace_entry f, gp_segment_configuration c where c.dbid = f.fsedbid
`

	// Execute the query
	cmdOut, err := executeOsCommandOutput("psql", "-p", i.GPInitSystem.MasterPort, "-d", "template1", "-Atc", queryString)
	if err != nil {
		Fatalf("Error in running uninstall command on database, err: %v", err)
	}

	// Create the file
	createFile(uninstallFile)
	writeFile(uninstallFile, []string{
		string(cmdOut),
		"rm -rf "+ Config.INSTALL.ENVDIR +"env_" + cmdOptions.Version + "_"+ i.Timestamp,
		"rm -rf " + uninstallFile,

	})
	return nil
}

// Uninstall gpcc
func (i *Installation) uninstallGPCCScript() error {
	i.GPCC.UninstallFile = Config.INSTALL.UNINTSALLDIR + fmt.Sprintf("uninstall_gpcc_%s_%s_%s", cmdOptions.Version, cmdOptions.CCVersion, i.Timestamp)
	Infof("Created uninstall script for this version of GPCC Installation: %s", i.GPCC.UninstallFile)
	writeFile(i.GPCC.UninstallFile, []string{
		"source " + i.EnvFile,
		"source " + i.GPCC.GpPerfmonHome + "/gpcc_path.sh",
		"gpcmdr --stop " + i.GPCC.InstanceName + " &>/dev/null",
		"gpcc stop " + i.GPCC.InstanceName + " &>/dev/null",
		"rm -rf " + i.GPCC.GpPerfmonHome + "/instances/" + i.GPCC.InstanceName,
		"gpconfig -c gp_enable_gpperfmon -v off &>/dev/null",
		"echo \"Stopping the database to cleanup any gpperfmon process\"",
		"gpstop -af &>/dev/null",
		"echo \"Starting the database\"",
		"gpstart -a &>/dev/null",
		"cp $MASTER_DATA_DIRECTORY/pg_hba.conf $MASTER_DATA_DIRECTORY/pg_hba.conf." + i.Timestamp + " &>/dev/null",
		"grep -v gpmon $MASTER_DATA_DIRECTORY/pg_hba.conf." + i.Timestamp + " > $MASTER_DATA_DIRECTORY/pg_hba.conf",
		"rm -rf $MASTER_DATA_DIRECTORY/pg_hba.conf." + i.Timestamp + " &>/dev/null",
		"psql -d template1 -p $PGPORT -Atc \"drop database gpperfmon\" &>/dev/null",
		"psql -d template1 -p $PGPORT -Atc \"drop role gpmon\" &>/dev/null",
		"rm -rf $MASTER_DATA_DIRECTORY/gpperfmon/*",
		"cp " + i.EnvFile + " " + i.EnvFile + "." + i.Timestamp,
		"egrep -v \"GPCC_UNINSTALL_LOC|GPCCVersion|GPPERFMONHOME|GPCC_INSTANCE_NAME|GPCCPORT\" " + i.EnvFile + "." + i.Timestamp +" > " + i.EnvFile,
		"rm -rf " + i.EnvFile + "." + i.Timestamp,
		"rm -rf " + i.GPCC.UninstallFile,
	})
	return nil
}

// Uninstall using gpdeletesystem
func removeEnvGpDeleteSystem(envFile string) error {
	Info("Starting the database if stopped to run the gpdeletesystem on the environment")

	// Start the database if not started
	startDBifNotStarted(envFile)

	Infof("Calling gpdeletesystem to remove the environment: %s", envFile)

	// Write it to the file.
	file := Config.CORE.TEMPDIR + "run_deletesystem.sh"
	createFile(file)
	writeFile(file, []string{
		"source " + envFile,
		"gpdeletesystem -d $MASTER_DATA_DIRECTORY -f << EOF",
		"y",
		"y",
		"EOF",
	})
	_, err := executeOsCommandOutput("/bin/sh", file)
	if err != nil {
		deleteFile(file)
		return err
	}
	deleteFile(file)
	return nil
}

// Uninstall GPCC
func removeGPCC(envFile string) {
	Infof("Uninstalling the version of command center that is currently installed on this environment.")
	executeOsCommand("/bin/sh", environment(envFile).GpccUninstallLoc)
}

// Uninstall using manual method
func removeEnvManually(version, timestamp string) {
	uninstallScript := Config.INSTALL.UNINTSALLDIR + fmt.Sprintf("uninstall_%s_%s", version, timestamp)
	Infof("Cleaning up the extra files using the uninstall script: %s", uninstallScript)
	exists, err := doesFileOrDirExists(uninstallScript)
	if err != nil {
		Fatalf("error when trying to find the uninstaller file \"%s\", err: %v", uninstallScript, err)
	}
	if exists {
		executeOsCommandOutput("/bin/sh", uninstallScript)
	} else {
		Fatalf("Unable to find the uninstaller file \"%s\"", uninstallScript)
	}
}

// Main Remove method
func remove() {
	Infof("Starting program to uninstall the version: %s", cmdOptions.Version)

	// Check if the envfile for that version exists
	chosenEnvFile := installedEnvFiles(fmt.Sprintf("*%s*", cmdOptions.Version), "choose", true)

	// If we receive none, then display the error to user
	var timestamp, version string
	if IsValueEmpty(chosenEnvFile) {
		Fatalf("Cannot find any environment with the version: %s", cmdOptions.Version)
	} else { // Else store the value
		timestamp = strings.Split(chosenEnvFile, "_")[2]
		version = strings.Split(chosenEnvFile, "_")[1]
	}
	Infof("The choosen enviornment file to remove is: %s ", chosenEnvFile)
	Info("Uninstalling the environment")

	// If there is failure in gpstart, user can use force to force manual uninstallation
	if !cmdOptions.Force {
		err := removeEnvGpDeleteSystem(chosenEnvFile)
		if err != nil {
			Warnf("Failed to uninstall using gpdeletesystem, trying manual method..")
		}
	} else {
		Infof("Forcing uninstall of the environment: %s", chosenEnvFile)
	}

	// Uninstall GPPC
	removeGPCC(chosenEnvFile)

	// Run this to cleanup the file created by go-gpdb
	removeEnvManually(version, timestamp)

	Infof("Uninstallation of environment \"%s\" was a success", chosenEnvFile)
	Info("exiting ....")
}<|endoftext|>


<commit_before>package main

import (
	"testing"
	"regifted"

)

func TestReadVideo(t *testing.T) {
	in := []byte{2, 5, 6, 3, 4, 5, 6, 5, 4, 3, 4, 0, 0, 0, 1, 8, 8, 4, 6, 5, 8, 3, 9, 2, 0, 4, 9, 5, 8, 0, 0, 0, 1, 9, 8, 2, 7, 4, 5, 2, 7, 4, 8, 9, 3}
	out := []byte{ 2, 5, 6, 3, 4, 5, 6, 5, 4, 3, 4, 8, 8, 4, 6, 5, 8, 3, 9, 2, 0, 4, 9, 5, 8, 9, 8, 2, 7, 4, 5, 2, 7, 4, 8, 9, 3 }

	n := new(Nal)

	if x := n.readVideo(in); x != out {
		t.Fail()
	}

}<commit_msg>minor change<commit_after>package main

import (
	"testing"
	"regifted/ts"

)

func TestReadVideo(t *testing.T) {
	in := []byte{2, 5, 6, 3, 4, 5, 6, 5, 4, 3, 4, 0, 0, 0, 1, 8, 8, 4, 6, 5, 8, 3, 9, 2, 0, 4, 9, 5, 8, 0, 0, 0, 1, 9, 8, 2, 7, 4, 5, 2, 7, 4, 8, 9, 3}
	out := []byte{ 2, 5, 6, 3, 4, 5, 6, 5, 4, 3, 4, 8, 8, 4, 6, 5, 8, 3, 9, 2, 0, 4, 9, 5, 8, 9, 8, 2, 7, 4, 5, 2, 7, 4, 8, 9, 3 }

	n := new(Nal)

	if x := n.readVideo(in); x != out {
		t.Fail()
	}

}<|endoftext|>


<commit_before>package graph

import (
	"testing"
	//"fmt"
	//"sync"
	//"github.com/stretchr/testify/assert"
	api "github.com/anemos-io/engine/grpc/anemos/v1alpha1"
	"github.com/anemos-io/engine/router"
	//"fmt"
	"github.com/stretchr/testify/assert"
	//"sync"
	//"fmt"
	"github.com/anemos-io/engine"

	"github.com/anemos-io/engine/provider/noop"
)

func NewSuccessTask(name string) (*TaskNode) {
	node := NewTaskNode()
	node.Provider = "anemos"
	node.Operation = "noop"
	node.Name = name

	return node
}

func StartGroupForSuccess(g *Group, expectSuccess bool, t *testing.T) {
	event := api.Event{
		Uri: "anemos/event:manual",
	}

	go g.OnEvent(&event)
	assert.Equal(t, expectSuccess, <-g.channel)

}

func TestTwoTasks(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	LinkDown(task1, task2)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
}

func TestSimpleSplit(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	LinkDown(task1, task2)
	LinkDown(task1, task3)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, task3.Status())
}

func TestSimpleJoin(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	LinkDown(task1, task3)
	LinkDown(task2, task3)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, task3.Status())
}

func TestSimpleSplitAndJoin(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	task4 := NewSuccessTask("task4")
	LinkDown(task1, task2)
	LinkDown(task1, task3)
	LinkDown(task2, task4)
	LinkDown(task3, task4)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.AddNode(task4)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, task3.Status())
	assert.Equal(t, anemos.Success, task4.Status())
}

func TestSingleFail(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task1.Attributes[noop.AttrNameRetries] = "1"

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, false, t)

	assert.Equal(t, anemos.Fail, task1.Status())
}

func TestFailPropagation(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task1.Attributes[noop.AttrNameRetries] = "1"
	task2 := NewSuccessTask("task2")
	//task2.Attributes[noop.AttrNameRetries] = "1"
	LinkDown(task1, task2)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, false, t)

	assert.Equal(t, anemos.Fail, task1.Status())
}

//func TestStress(t *testing.T) {
//
//	r := router.NewInternalRouter()
//
//	//channel := make(chan *Task)
//	//end := make(chan bool)
//	//
//	//go PrintChannel(channel)
//
//	depth := 1000
//	hor := make([][]*TaskNode, depth*2)
//	for i := 0; i < depth; i++ {
//		hor[i] = make([]*TaskNode, i+1)
//		hor[depth*2-1-i] = make([]*TaskNode, i+1)
//	}
//
//	g := NewGroup()
//	g.Name = "group"
//
//	hor[0][0] = NewSuccessTask("(0,0)")
//	g.AddNode(hor[0][0])
//
//	for i := 1; i < depth; i++ {
//		for j := 0; j <= i; j++ {
//			name := fmt.Sprintf("(%d,%d)", i, j)
//			//fmt.Println(name)
//
//			node := NewSuccessTask(name)
//			if j < i {
//				LinkDown(hor[i-1][j], node)
//			}
//			if j > 0 {
//				LinkDown(hor[i-1][j-1], node)
//			}
//			hor[i][j] = node
//			g.AddNode(node)
//
//		}
//	}
//
//	g.Resolve()
//	r.RegisterGroup(g)
//	StartGroupForSuccess(g, t)
//}
<commit_msg>Deeper unit testing of graph<commit_after>package graph

import (
	"testing"
	//"fmt"
	//"sync"
	//"github.com/stretchr/testify/assert"
	api "github.com/anemos-io/engine/grpc/anemos/v1alpha1"
	"github.com/anemos-io/engine/router"
	//"fmt"
	"github.com/stretchr/testify/assert"
	//"sync"
	//"fmt"
	"github.com/anemos-io/engine"

	"github.com/anemos-io/engine/provider/noop"
)

func NewSuccessTask(name string) (*TaskNode) {
	node := NewTaskNode()
	node.Provider = "anemos"
	node.Operation = "noop"
	node.Name = name

	return node
}

func StartGroupForSuccess(g *Group, expectSuccess bool, t *testing.T) {
	event := api.Event{
		Uri: "anemos/event:manual",
	}

	go g.OnEvent(&event)
	assert.Equal(t, expectSuccess, <-g.channel)
}

func TestTwoTasks(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	LinkDown(task1, task2)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, g.Status())
}

func TestSimpleSplit(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	LinkDown(task1, task2)
	LinkDown(task1, task3)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, task3.Status())
	assert.Equal(t, anemos.Success, g.Status())
}

func TestSimpleJoin(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	LinkDown(task1, task3)
	LinkDown(task2, task3)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, task3.Status())
	assert.Equal(t, anemos.Success, g.Status())
}

func TestSimpleSplitAndJoin(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	task4 := NewSuccessTask("task4")
	LinkDown(task1, task2)
	LinkDown(task1, task3)
	LinkDown(task2, task4)
	LinkDown(task3, task4)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.AddNode(task4)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, true, t)

	assert.Equal(t, anemos.Success, task1.Status())
	assert.Equal(t, anemos.Success, task2.Status())
	assert.Equal(t, anemos.Success, task3.Status())
	assert.Equal(t, anemos.Success, task4.Status())
	assert.Equal(t, anemos.Success, g.Status())
}

func TestSingleFail(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task1.Attributes[noop.AttrNameRetries] = "1"

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, false, t)

	assert.Equal(t, anemos.Fail, task1.Status())
	assert.Equal(t, anemos.Fail, g.Status())
}

func TestFailPropagation(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task1.Attributes[noop.AttrNameRetries] = "1"
	task2 := NewSuccessTask("task2")
	//task2.Attributes[noop.AttrNameRetries] = "1"
	LinkDown(task1, task2)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, false, t)

	assert.Equal(t, anemos.Fail, task1.Status())
	assert.Equal(t, anemos.Fail, task2.Status())
	assert.Equal(t, anemos.Fail, g.Status())
}

func TestFailSplitPropagation(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task1.Attributes[noop.AttrNameRetries] = "1"
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	//task2.Attributes[noop.AttrNameRetries] = "1"
	LinkDown(task1, task2)
	LinkDown(task1, task3)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, false, t)

	assert.Equal(t, anemos.Fail, task1.Status())
	assert.Equal(t, anemos.Fail, task2.Status())
	assert.Equal(t, anemos.Fail, task3.Status())
	assert.Equal(t, anemos.Fail, g.Status())
}

func TestFailSplitJoinPropagation(t *testing.T) {

	r := router.NewInternalRouter()

	task1 := NewSuccessTask("task1")
	task1.Attributes[noop.AttrNameRetries] = "1"
	task2 := NewSuccessTask("task2")
	task3 := NewSuccessTask("task3")
	task4 := NewSuccessTask("task4")
	//task2.Attributes[noop.AttrNameRetries] = "1"
	LinkDown(task1, task2)
	LinkDown(task1, task3)

	g := NewGroup()
	g.Name = "group"

	g.AddNode(task1)
	g.AddNode(task2)
	g.AddNode(task3)
	g.AddNode(task4)
	g.Resolve()

	r.RegisterGroup(g)

	StartGroupForSuccess(g, false, t)

	assert.Equal(t, anemos.Fail, task1.Status())
	assert.Equal(t, anemos.Fail, task2.Status())
	assert.Equal(t, anemos.Fail, task3.Status())
	assert.Equal(t, anemos.Fail, g.Status())
}

//func TestStress(t *testing.T) {
//
//	r := router.NewInternalRouter()
//
//	//channel := make(chan *Task)
//	//end := make(chan bool)
//	//
//	//go PrintChannel(channel)
//
//	depth := 1000
//	hor := make([][]*TaskNode, depth*2)
//	for i := 0; i < depth; i++ {
//		hor[i] = make([]*TaskNode, i+1)
//		hor[depth*2-1-i] = make([]*TaskNode, i+1)
//	}
//
//	g := NewGroup()
//	g.Name = "group"
//
//	hor[0][0] = NewSuccessTask("(0,0)")
//	g.AddNode(hor[0][0])
//
//	for i := 1; i < depth; i++ {
//		for j := 0; j <= i; j++ {
//			name := fmt.Sprintf("(%d,%d)", i, j)
//			//fmt.Println(name)
//
//			node := NewSuccessTask(name)
//			if j < i {
//				LinkDown(hor[i-1][j], node)
//			}
//			if j > 0 {
//				LinkDown(hor[i-1][j-1], node)
//			}
//			hor[i][j] = node
//			g.AddNode(node)
//
//		}
//	}
//
//	g.Resolve()
//	r.RegisterGroup(g)
//	StartGroupForSuccess(g, t)
//}
<|endoftext|>


<commit_before>// Copyright 2016 Arne Roomann-Kurrik
//
// 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 render

import (
	"fmt"
	"github.com/go-gl/gl/v3.3-core/gl"
	"github.com/go-gl/mathgl/mgl32"
	"github.com/kurrik/opengl-benchmarks/common"
	"github.com/kurrik/opengl-benchmarks/common/sprites"
	"unsafe"
)

const FRAGMENT = `#version 150

precision mediump float;

in vec2 v_TexturePos;
in vec2 v_TextureMin;
in vec2 v_TextureDim;
uniform sampler2D u_Texture;
out vec4 v_FragData;

void main() {
  vec2 v_TexturePosition = v_TextureMin + mod(v_TexturePos, v_TextureDim);
  v_FragData = texture(u_Texture, v_TexturePosition);
}`

const VERTEX = `#version 150

#define MAX_TILES 1024

struct Tile {
  vec4 texture;
};

layout (std140) uniform TextureData {
  Tile Tiles[MAX_TILES];
};

in vec3 v_Position;
in vec2 v_Texture;
in float f_VertexFrame;
in float f_InstanceFrame;
in mat4 m_Model;
uniform mat4 m_View;
uniform mat4 m_Projection;
out vec2 v_TexturePos;
out vec2 v_TextureMin;
out vec2 v_TextureDim;

void main() {
  Tile t_Tile = Tiles[int(f_VertexFrame + f_InstanceFrame)];
  v_TextureMin = t_Tile.texture.zw;
  v_TextureDim = t_Tile.texture.xy;
  v_TexturePos = v_Texture * v_TextureDim;
  gl_Position = m_Projection * m_View * m_Model * vec4(v_Position, 1.0);
}`

type renderInstance struct {
	model mgl32.Mat4
	frame float32
}

type Renderer struct {
	shader     *common.Program
	vbo        *common.ArrayBuffer
	ubo        *common.UniformBuffer
	uView      *common.Uniform
	uProj      *common.Uniform
	bufferSize int
	buffer     []renderInstance
	stride     uintptr
}

func NewRenderer(bufferSize int) (r *Renderer, err error) {
	var (
		instance       renderInstance
		instanceStride = unsafe.Sizeof(instance)
	)
	r = &Renderer{
		shader:     common.NewProgram(),
		bufferSize: bufferSize,
		buffer:     make([]renderInstance, bufferSize),
		stride:     instanceStride,
	}
	if err = r.shader.Load(VERTEX, FRAGMENT); err != nil {
		return
	}
	r.shader.Bind()

	r.vbo = common.NewArrayBuffer()
	r.ubo = common.NewUniformBuffer(r.shader.ID())
	r.ubo.BlockBinding("TextureData", 1)

	r.shader.Attrib("f_InstanceFrame", instanceStride).Float(unsafe.Offsetof(instance.frame), 1)
	r.shader.Attrib("m_Model", instanceStride).Mat4(unsafe.Offsetof(instance.model), 1)

	r.uView = r.shader.Uniform("m_View")
	r.uProj = r.shader.Uniform("m_Projection")

	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR: OpenGL error %X", e)
	}
	return
}

func (r *Renderer) Bind() {
	r.shader.Bind()
}

func (r *Renderer) Register(geometry *Geometry) {
	var (
		pt       Point
		ptStride = unsafe.Sizeof(pt)
	)
	r.shader.Bind()
	geometry.Bind()
	r.shader.Attrib("v_Position", ptStride).Vec3(unsafe.Offsetof(pt.Position), 0)
	r.shader.Attrib("v_Texture", ptStride).Vec2(unsafe.Offsetof(pt.Texture), 0)
	r.shader.Attrib("f_VertexFrame", ptStride).Float(unsafe.Offsetof(pt.Frame), 0)
}

func (r *Renderer) Unbind() {
	r.shader.Unbind()
}

func (r *Renderer) Delete() {
	if r.shader != nil {
		r.shader.Delete()
		r.shader = nil
	}
	if r.ubo != nil {
		r.ubo.Delete()
		r.ubo = nil
	}
	if r.vbo != nil {
		r.vbo.Delete()
		r.vbo = nil
	}
}

func (r *Renderer) draw(geometry *Geometry, count int) (err error) {
	if count <= 0 {
		return
	}
	r.vbo.Upload(r.buffer, count*int(r.stride))
	gl.DrawArraysInstanced(gl.TRIANGLES, 0, int32(len(geometry.Points)), int32(count))
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR: OpenGL error %X", e)
	}
	return
}

func (r *Renderer) Render(
	camera *common.Camera,
	sheet sprites.UniformBufferSheet,
	geometry *Geometry,
	instances *InstanceList,
) (err error) {
	var (
		instance *Instance
		i        *renderInstance
		index    int
	)
	r.uView.Mat4(camera.View)
	r.uProj.Mat4(camera.Projection)
	sheet.Upload(r.ubo)
	geometry.Bind()
	geometry.Upload()
	r.Register(geometry)
	index = 0
	instance = instances.Head()
	for instance != nil {
		i = &r.buffer[index]
		i.frame = float32(instance.Frame)
		i.model = instance.GetModel()
		index++
		instance = instance.Next()
		if index >= r.bufferSize {
			if err = r.draw(geometry, index); err != nil {
				return
			}
			index = 0
		}
	}
	err = r.draw(geometry, index)
	return
}
<commit_msg>Remove unneeded call<commit_after>// Copyright 2016 Arne Roomann-Kurrik
//
// 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 render

import (
	"fmt"
	"github.com/go-gl/gl/v3.3-core/gl"
	"github.com/go-gl/mathgl/mgl32"
	"github.com/kurrik/opengl-benchmarks/common"
	"github.com/kurrik/opengl-benchmarks/common/sprites"
	"unsafe"
)

const FRAGMENT = `#version 150

precision mediump float;

in vec2 v_TexturePos;
in vec2 v_TextureMin;
in vec2 v_TextureDim;
uniform sampler2D u_Texture;
out vec4 v_FragData;

void main() {
  vec2 v_TexturePosition = v_TextureMin + mod(v_TexturePos, v_TextureDim);
  v_FragData = texture(u_Texture, v_TexturePosition);
}`

const VERTEX = `#version 150

#define MAX_TILES 1024

struct Tile {
  vec4 texture;
};

layout (std140) uniform TextureData {
  Tile Tiles[MAX_TILES];
};

in vec3 v_Position;
in vec2 v_Texture;
in float f_VertexFrame;
in float f_InstanceFrame;
in mat4 m_Model;
uniform mat4 m_View;
uniform mat4 m_Projection;
out vec2 v_TexturePos;
out vec2 v_TextureMin;
out vec2 v_TextureDim;

void main() {
  Tile t_Tile = Tiles[int(f_VertexFrame + f_InstanceFrame)];
  v_TextureMin = t_Tile.texture.zw;
  v_TextureDim = t_Tile.texture.xy;
  v_TexturePos = v_Texture * v_TextureDim;
  gl_Position = m_Projection * m_View * m_Model * vec4(v_Position, 1.0);
}`

type renderInstance struct {
	model mgl32.Mat4
	frame float32
}

type Renderer struct {
	shader     *common.Program
	vbo        *common.ArrayBuffer
	ubo        *common.UniformBuffer
	uView      *common.Uniform
	uProj      *common.Uniform
	bufferSize int
	buffer     []renderInstance
	stride     uintptr
}

func NewRenderer(bufferSize int) (r *Renderer, err error) {
	var (
		instance       renderInstance
		instanceStride = unsafe.Sizeof(instance)
	)
	r = &Renderer{
		shader:     common.NewProgram(),
		bufferSize: bufferSize,
		buffer:     make([]renderInstance, bufferSize),
		stride:     instanceStride,
	}
	if err = r.shader.Load(VERTEX, FRAGMENT); err != nil {
		return
	}
	r.shader.Bind()

	r.vbo = common.NewArrayBuffer()
	r.ubo = common.NewUniformBuffer(r.shader.ID())
	r.ubo.BlockBinding("TextureData", 1)

	r.shader.Attrib("f_InstanceFrame", instanceStride).Float(unsafe.Offsetof(instance.frame), 1)
	r.shader.Attrib("m_Model", instanceStride).Mat4(unsafe.Offsetof(instance.model), 1)

	r.uView = r.shader.Uniform("m_View")
	r.uProj = r.shader.Uniform("m_Projection")

	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR: OpenGL error %X", e)
	}
	return
}

func (r *Renderer) Bind() {
	r.shader.Bind()
}

func (r *Renderer) register(geometry *Geometry) {
	var (
		pt       Point
		ptStride = unsafe.Sizeof(pt)
	)
	geometry.Bind()
	r.shader.Attrib("v_Position", ptStride).Vec3(unsafe.Offsetof(pt.Position), 0)
	r.shader.Attrib("v_Texture", ptStride).Vec2(unsafe.Offsetof(pt.Texture), 0)
	r.shader.Attrib("f_VertexFrame", ptStride).Float(unsafe.Offsetof(pt.Frame), 0)
}

func (r *Renderer) Unbind() {
	r.shader.Unbind()
}

func (r *Renderer) Delete() {
	if r.shader != nil {
		r.shader.Delete()
		r.shader = nil
	}
	if r.ubo != nil {
		r.ubo.Delete()
		r.ubo = nil
	}
	if r.vbo != nil {
		r.vbo.Delete()
		r.vbo = nil
	}
}

func (r *Renderer) draw(geometry *Geometry, count int) (err error) {
	if count <= 0 {
		return
	}
	r.vbo.Upload(r.buffer, count*int(r.stride))
	gl.DrawArraysInstanced(gl.TRIANGLES, 0, int32(len(geometry.Points)), int32(count))
	if e := gl.GetError(); e != 0 {
		err = fmt.Errorf("ERROR: OpenGL error %X", e)
	}
	return
}

func (r *Renderer) Render(
	camera *common.Camera,
	sheet sprites.UniformBufferSheet,
	geometry *Geometry,
	instances *InstanceList,
) (err error) {
	var (
		instance *Instance
		i        *renderInstance
		index    int
	)
	r.uView.Mat4(camera.View)
	r.uProj.Mat4(camera.Projection)
	sheet.Upload(r.ubo)
	geometry.Upload()
	r.register(geometry)
	index = 0
	instance = instances.Head()
	for instance != nil {
		i = &r.buffer[index]
		i.frame = float32(instance.Frame)
		i.model = instance.GetModel()
		index++
		instance = instance.Next()
		if index >= r.bufferSize {
			if err = r.draw(geometry, index); err != nil {
				return
			}
			index = 0
		}
	}
	err = r.draw(geometry, index)
	return
}
<|endoftext|>


<commit_before>package tests

import (
	"errors"
	"fmt"
	"koding/db/mongodb/modelhelper"
	"socialapi/config"
	"socialapi/models"
	"socialapi/request"
	"socialapi/rest"
	"socialapi/workers/integration/webhook"
	"socialapi/workers/integration/webhook/api"
	"testing"
	"time"

	"github.com/koding/runner"
	. "github.com/smartystreets/goconvey/convey"
)

func newPushRequest(body string) *api.PushRequest {
	wr := &api.PushRequest{
		Message: webhook.Message{
			Body: body,
		},
	}

	return wr
}

func newBotChannelRequest(nick, groupName string) *api.BotChannelRequest {
	return &api.BotChannelRequest{
		GroupName: groupName,
		Username:  nick,
	}
}

func TestWebhook(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()

	appConfig := config.MustRead(r.Conf.Path)
	modelhelper.Initialize(appConfig.Mongo)
	defer modelhelper.Close()

	Convey("We should be able to successfully push message", t, func() {
		channelIntegration, topicChannel := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick("sinan")
		So(err, ShouldBeNil)

		_, err = topicChannel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		err = rest.DoPushRequest(newPushRequest(models.RandomName()), channelIntegration.Token)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, channelIntegration.GroupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		resp, err := rest.GetHistory(channelIntegration.ChannelId,
			&request.Query{
				AccountId: account.Id,
			},
			ses.ClientId,
		)

		So(err, ShouldBeNil)
		So(len(resp.MessageList), ShouldEqual, 1)
	})

	Convey("We should be able to successfully fetch bot channel of the user", t, func() {
		account, err := models.CreateAccountInBothDbsWithNick("sinan")
		So(err, ShouldBeNil)
		groupName := models.RandomGroupName()
		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_GROUP, groupName)
		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, groupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		channelId, err := rest.DoBotChannelRequest(ses.ClientId)

		So(err, ShouldBeNil)
		So(channelId, ShouldNotEqual, 0)
	})

	Convey("We should be able to successfully receive github push messages via middleware", t, func() {
		channelIntegration, topicChannel := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick(models.RandomName())
		So(err, ShouldBeNil)

		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_GROUP, channelIntegration.GroupName)

		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)
		_, err = topicChannel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		err = rest.DoGithubPush(githubPushEventData, channelIntegration.Token)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, channelIntegration.GroupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		tick := time.Tick(time.Millisecond * 200)
		deadLine := time.After(10 * time.Second)
		for {
			select {
			case <-tick:
				resp, err := rest.GetHistory(topicChannel.Id,
					&request.Query{},
					ses.ClientId,
				)
				So(err, ShouldBeNil)
				if len(resp.MessageList) > 0 {
					So(len(resp.MessageList), ShouldEqual, 1)
					So(resp.MessageList[0].Message.Body, ShouldStartWith, "[canthefason](https://github.com/canthefason) [pushed]")
					return
				}
			case <-deadLine:
				So(errors.New("Could not fetch messages"), ShouldBeNil)
			}
		}

	})

	Convey("We should not be able to send more than 100 requests per minute", t, func() {
		channelIntegration, _ := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick("sinan")
		So(err, ShouldBeNil)

		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_TOPIC, channelIntegration.GroupName)
		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		name := models.RandomName()
		for i := 0; i < 100; i++ {
			err = rest.DoPushRequest(newPushRequest(name), channelIntegration.Token)
			if err != nil {
				fmt.Println("i ver", i)
			}
			So(err, ShouldBeNil)
		}

		err = rest.DoPushRequest(newPushRequest(name), channelIntegration.Token)
		So(err, ShouldNotBeNil)

	})

}
<commit_msg>pivotal: pivotal test for integration is added<commit_after>package tests

import (
	"errors"
	"fmt"
	"koding/db/mongodb/modelhelper"
	"socialapi/config"
	"socialapi/models"
	"socialapi/request"
	"socialapi/rest"
	"socialapi/workers/integration/webhook"
	"socialapi/workers/integration/webhook/api"
	"testing"
	"time"

	"github.com/koding/runner"
	. "github.com/smartystreets/goconvey/convey"
)

func newPushRequest(body string) *api.PushRequest {
	wr := &api.PushRequest{
		Message: webhook.Message{
			Body: body,
		},
	}

	return wr
}

func newBotChannelRequest(nick, groupName string) *api.BotChannelRequest {
	return &api.BotChannelRequest{
		GroupName: groupName,
		Username:  nick,
	}
}

func TestWebhook(t *testing.T) {
	r := runner.New("test")
	if err := r.Init(); err != nil {
		t.Fatalf("couldnt start bongo %s", err.Error())
	}
	defer r.Close()

	appConfig := config.MustRead(r.Conf.Path)
	modelhelper.Initialize(appConfig.Mongo)
	defer modelhelper.Close()

	Convey("We should be able to successfully push message", t, func() {
		channelIntegration, topicChannel := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick("sinan")
		So(err, ShouldBeNil)

		_, err = topicChannel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		err = rest.DoPushRequest(newPushRequest(models.RandomName()), channelIntegration.Token)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, channelIntegration.GroupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		resp, err := rest.GetHistory(channelIntegration.ChannelId,
			&request.Query{
				AccountId: account.Id,
			},
			ses.ClientId,
		)

		So(err, ShouldBeNil)
		So(len(resp.MessageList), ShouldEqual, 1)
	})

	Convey("We should be able to successfully fetch bot channel of the user", t, func() {
		account, err := models.CreateAccountInBothDbsWithNick("sinan")
		So(err, ShouldBeNil)
		groupName := models.RandomGroupName()
		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_GROUP, groupName)
		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, groupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		channelId, err := rest.DoBotChannelRequest(ses.ClientId)

		So(err, ShouldBeNil)
		So(channelId, ShouldNotEqual, 0)
	})

	Convey("We should be able to successfully receive github push messages via middleware", t, func() {
		channelIntegration, topicChannel := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick(models.RandomName())
		So(err, ShouldBeNil)

		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_GROUP, channelIntegration.GroupName)

		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)
		_, err = topicChannel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		err = rest.DoGithubPush(githubPushEventData, channelIntegration.Token)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, channelIntegration.GroupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		tick := time.Tick(time.Millisecond * 200)
		deadLine := time.After(10 * time.Second)
		for {
			select {
			case <-tick:
				resp, err := rest.GetHistory(topicChannel.Id,
					&request.Query{},
					ses.ClientId,
				)
				So(err, ShouldBeNil)
				if len(resp.MessageList) > 0 {
					So(len(resp.MessageList), ShouldEqual, 1)
					So(resp.MessageList[0].Message.Body, ShouldStartWith, "[canthefason](https://github.com/canthefason) [pushed]")
					return
				}
			case <-deadLine:
				So(errors.New("Could not fetch messages"), ShouldBeNil)
			}
		}

	})

	Convey("We should be able to successfully receive pivotal push messages via middleware", t, func() {
		channelIntegration, topicChannel := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick(models.RandomName())
		So(err, ShouldBeNil)

		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_GROUP, channelIntegration.GroupName)

		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)
		_, err = topicChannel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		err = rest.DoPivotalPush("POST", pivotalEventData, channelIntegration.Token)
		So(err, ShouldBeNil)

		ses, err := models.FetchOrCreateSession(account.Nick, channelIntegration.GroupName)
		So(err, ShouldBeNil)
		So(ses, ShouldNotBeNil)

		tick := time.Tick(time.Millisecond * 200)
		deadLine := time.After(10 * time.Second)
		for {
			select {
			case <-tick:
				resp, err := rest.GetHistory(topicChannel.Id,
					&request.Query{},
					ses.ClientId,
				)
				So(err, ShouldBeNil)
				if len(resp.MessageList) > 0 {
					So(len(resp.MessageList), ShouldEqual, 1)
					So(resp.MessageList[0].Message.Body, ShouldStartWith, "Mehmet Ali Savas started this feature")
					return
				}
			case <-deadLine:
				So(errors.New("Could not fetch messages"), ShouldBeNil)
			}
		}

	})

	Convey("We should not be able to send more than 100 requests per minute", t, func() {
		channelIntegration, _ := webhook.CreateTestChannelIntegration(t)

		account, err := models.CreateAccountInBothDbsWithNick("sinan")
		So(err, ShouldBeNil)

		channel := models.CreateTypedGroupedChannelWithTest(account.Id, models.Channel_TYPE_TOPIC, channelIntegration.GroupName)
		_, err = channel.AddParticipant(account.Id)
		So(err, ShouldBeNil)

		name := models.RandomName()
		for i := 0; i < 100; i++ {
			err = rest.DoPushRequest(newPushRequest(name), channelIntegration.Token)
			if err != nil {
				fmt.Println("i ver", i)
			}
			So(err, ShouldBeNil)
		}

		err = rest.DoPushRequest(newPushRequest(name), channelIntegration.Token)
		So(err, ShouldNotBeNil)

	})

}
<|endoftext|>