<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"regexp"
)

type karmas struct {
	karmaMap *map[string]karmaSet
}

type karmaSet struct {
	plusplus   int
	minusminus int
	plusminus  int
}

func (k karmaSet) value() int {
	return k.plusplus - k.minusminus
}

func (k karmaSet) String() string {
	return fmt.Sprintf("(%v++,%v--,%v+-)", k.plusplus, k.minusminus, k.plusminus)
}

var regex = regexp.MustCompile("([^ ]+)(\\+\\+|--|\\+-|-\\+)")
var getkarma = regexp.MustCompile("^!karma +([^ ]+)")

func main() {
	k := newKarmas()
	http.Handle("/", k)

	http.ListenAndServe(":8080", nil)
}

func newKarmas() karmas {
	m := make(map[string]karmaSet)
	return karmas{&m}
}

func (k karmas) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	text := r.Form.Get("text")
	matches := regex.FindAllStringSubmatch(text, -1)
	karma := getkarma.FindStringSubmatch(text)
	if matches != nil && r.Form.Get("user_name") != "slackbot" {
		for _, match := range matches {
			key := match[1]
			op := match[2]
			set := (*k.karmaMap)[key]
			if key != "" {
				switch op {
				case "--":
					set.minusminus++
				case "++":
					set.plusplus++
				case "-+", "+-":
					set.plusminus++
				}
				(*k.karmaMap)[key] = set
			}
		}
		fmt.Println(*k.karmaMap)
	} else if karma != nil {
		name := karma[1]
		fmt.Println("asking for", name)
		res := make(map[string]string)
		karmaset := (*k.karmaMap)[name]
		res["text"] = fmt.Sprintf("%v: %v %v", r.Form.Get("user_name"), karmaset.value(), karmaset)
		res["parse"] = "full"
		res["username"] = "dabopobo"
		resp, _ := json.Marshal(res)
		fmt.Println(string(resp))
		w.WriteHeader(200)
		w.Write(resp)
	}
}
<commit_msg>redis and shit<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strconv"

	"github.com/xuyu/goredis"
)

type state struct {
	redis *goredis.Redis
}

type karmaSet struct {
	plusplus    int
	minusminus  int
	plusminus   int
	minusequals int
	plusequals  int
}

func (k karmaSet) value() int {
	return k.plusplus - k.minusminus
}

func (k karmaSet) String() string {
	return fmt.Sprintf("(%v++,%v--,%v+-,%v+=,%v-=)", k.plusplus, k.minusminus, k.plusminus, k.plusequals, k.minusequals)
}

var indentifierRegex = regexp.MustCompile("([^ ]+)(\\+\\+|--|\\+-|-\\+|-=|\\+=)")
var getkarma = regexp.MustCompile("^!karma +([^ ]+)")

func main() {
	redis, err := goredis.Dial(&goredis.DialConfig{Address: "127.0.0.1:6379"})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	s := state{redis}
	http.Handle("/", s)

	http.ListenAndServe(":8080", nil)
}

func (s state) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	text := r.Form.Get("text")
	indentifierMatches := indentifierRegex.FindAllStringSubmatch(text, -1)
	karma := getkarma.FindStringSubmatch(text)
	username := r.Form.Get("user_name")
	if indentifierMatches != nil && username != "slackbot" {
		for _, match := range indentifierMatches {
			key := match[1]
			op := match[2]
			if key != "" && key != username {
				var err error
				switch op {
				case "--":
					_, err = s.redis.Incr(key + "--")
				case "++":
					_, err = s.redis.Incr(key + "++")
				case "-+", "+-":
					_, err = s.redis.Incr(key + "+-")
				case "+=":
					_, err = s.redis.Incr(key + "+=")
				case "-=":
					_, err = s.redis.Incr(key + "-=")
				}
				fmt.Fprintln(os.Stderr, err)
				if err != nil {
					panic(err)
				}
			}
		}
	} else if karma != nil {
		name := karma[1]
		fmt.Println("asking for", name)
		res := make(map[string]string)
		karmaset := s.getKarmaSet(name)
		res["text"] = fmt.Sprintf("%v's karma is %v %v", name, karmaset.value(), karmaset)
		res["parse"] = "full"
		res["username"] = "dabopobo"
		resp, _ := json.Marshal(res)
		fmt.Println(string(resp))
		w.WriteHeader(200)
		w.Write(resp)
	}
}

func (s state) getKarmaSet(name string) (k karmaSet) {
	k.plusplus = getRedisInt(s.redis, name+"++", 0)
	k.minusminus = getRedisInt(s.redis, name+"--", 0)
	k.plusminus = getRedisInt(s.redis, name+"+-", 0)
	k.plusequals = getRedisInt(s.redis, name+"+=", 0)
	k.minusequals = getRedisInt(s.redis, name+"-=", 0)
	return
}

func getRedisInt(r *goredis.Redis, key string, def int) int {
	val, err := r.Get(key)
	if err != nil {
		return def
	}

	value, err := strconv.Atoi(string(val))
	if err != nil {
		return def
	}

	return value
}
<|endoftext|>


<commit_before>// Copyright © 2017 The Kubicorn 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 amazon

import (
	"fmt"

	"github.com/kris-nova/kubicorn/apis/cluster"
	"github.com/kris-nova/kubicorn/cutil/kubeadm"
	"github.com/kris-nova/kubicorn/cutil/uuid"
)

// NewUbuntuCluster creates a simple Ubuntu Amazon cluster
func NewUbuntuCluster(name string) *cluster.Cluster {
	return &cluster.Cluster{
		Name:     name,
		Cloud:    cluster.CloudAmazon,
		Location: "us-west-2",
		SSH: &cluster.SSH{
			PublicKeyPath: "~/.ssh/id_rsa.pub",
			User:          "ubuntu",
		},
		KubernetesAPI: &cluster.KubernetesAPI{
			Port: "443",
		},
		Network: &cluster.Network{
			Type:       cluster.NetworkTypePublic,
			CIDR:       "10.0.0.0/16",
			InternetGW: &cluster.InternetGW{},
		},
		Values: &cluster.Values{
			ItemMap: map[string]string{
				"INJECTEDTOKEN": kubeadm.GetRandomToken(),
			},
		},
		ServerPools: []*cluster.ServerPool{
			{
				Type:     cluster.ServerPoolTypeMaster,
				Name:     fmt.Sprintf("%s.master", name),
				MaxCount: 1,
				MinCount: 1,
				Image:    "ami-835b4efa",
				Size:     "t2.xlarge",
				BootstrapScripts: []string{
					"bootstrap/amazon_k8s_ubuntu_16.04_master.sh",
				},
				InstanceProfile: &cluster.IAMInstanceProfile{
					Name: fmt.Sprintf("%s-KubicornMasterInstanceProfile", name),
					Role: &cluster.IAMRole{
						Name: fmt.Sprintf("%s-KubicornMasterRole", name),
						Policies: []*cluster.IAMPolicy{
							{
								Name: "MasterPolicy",
								Document: `{
								  "Version": "2012-10-17",
								  "Statement": [
									 {
										"Effect": "Allow",
										"Action": [
										   "ec2:*",
										   "elasticloadbalancing:*",
										   "ecr:GetAuthorizationToken",
										   "ecr:BatchCheckLayerAvailability",
										   "ecr:GetDownloadUrlForLayer",
										   "ecr:GetRepositoryPolicy",
										   "ecr:DescribeRepositories",
										   "ecr:ListImages",
										   "ecr:BatchGetImage",
										   "autoscaling:DescribeAutoScalingGroups",
										   "autoscaling:UpdateAutoScalingGroup"
										],
										"Resource": "*"
									 }
								  ]
								}`,
							},
						},
					},
				},
				Subnets: []*cluster.Subnet{
					{
						Name: fmt.Sprintf("%s.master", name),
						CIDR: "10.0.0.0/24",
						Zone: "us-west-2a",
					},
				},
				AwsConfiguration: &cluster.AwsConfiguration{},
				Firewalls: []*cluster.Firewall{
					{
						Name: fmt.Sprintf("%s.master-external-%s", name, uuid.TimeOrderedUUID()),
						IngressRules: []*cluster.IngressRule{
							{
								IngressFromPort: "22",
								IngressToPort:   "22",
								IngressSource:   "0.0.0.0/0",
								IngressProtocol: "tcp",
							},
							{
								IngressFromPort: "443",
								IngressToPort:   "443",
								IngressSource:   "0.0.0.0/0",
								IngressProtocol: "tcp",
							},
							{
								IngressFromPort: "0",
								IngressToPort:   "65535",
								IngressSource:   "10.0.100.0/24",
								IngressProtocol: "-1",
							},
						},
					},
				},
			},
			{
				Type:     cluster.ServerPoolTypeNode,
				Name:     fmt.Sprintf("%s.node", name),
				MaxCount: 1,
				MinCount: 1,
				Image:    "ami-835b4efa",
				Size:     "t2.medium",
				BootstrapScripts: []string{
					"bootstrap/amazon_k8s_ubuntu_16.04_node.sh",
				},
				InstanceProfile: &cluster.IAMInstanceProfile{
					Name: fmt.Sprintf("%s-KubicornNodeInstanceProfile", name),
					Role: &cluster.IAMRole{
						Name: fmt.Sprintf("%s-KubicornNodeRole", name),
						Policies: []*cluster.IAMPolicy{
							{
								Name: "MasterPolicy",
								Document: `{
								  "Version": "2012-10-17",
								  "Statement": [
									 {
										"Effect": "Allow",
										"Action": [
										   "ec2:Describe*",
										   "ecr:GetAuthorizationToken",
										   "ecr:BatchCheckLayerAvailability",
										   "ecr:GetDownloadUrlForLayer",
										   "ecr:GetRepositoryPolicy",
										   "ecr:DescribeRepositories",
										   "ecr:ListImages",
										   "ecr:BatchGetImage",
										   "autoscaling:DescribeAutoScalingGroups",
										   "autoscaling:UpdateAutoScalingGroup"
										],
										"Resource": "*"
									 }
								  ]
								}`,
							},
						},
					},
				},
				Subnets: []*cluster.Subnet{
					{
						Name: fmt.Sprintf("%s.node", name),
						CIDR: "10.0.100.0/24",
						Zone: "us-west-2b",
					},
				},
				AwsConfiguration: &cluster.AwsConfiguration{},
				Firewalls: []*cluster.Firewall{
					{
						Name: fmt.Sprintf("%s.node-external-%s", name, uuid.TimeOrderedUUID()),
						IngressRules: []*cluster.IngressRule{
							{
								IngressFromPort: "22",
								IngressToPort:   "22",
								IngressSource:   "0.0.0.0/0",
								IngressProtocol: "tcp",
							},
							{
								IngressFromPort: "0",
								IngressToPort:   "65535",
								IngressSource:   "10.0.0.0/24",
								IngressProtocol: "-1",
							},
						},
					},
				},
			},
		},
	}
}
<commit_msg>Change policy name for node<commit_after>// Copyright © 2017 The Kubicorn 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 amazon

import (
	"fmt"

	"github.com/kris-nova/kubicorn/apis/cluster"
	"github.com/kris-nova/kubicorn/cutil/kubeadm"
	"github.com/kris-nova/kubicorn/cutil/uuid"
)

// NewUbuntuCluster creates a simple Ubuntu Amazon cluster
func NewUbuntuCluster(name string) *cluster.Cluster {
	return &cluster.Cluster{
		Name:     name,
		Cloud:    cluster.CloudAmazon,
		Location: "us-west-2",
		SSH: &cluster.SSH{
			PublicKeyPath: "~/.ssh/id_rsa.pub",
			User:          "ubuntu",
		},
		KubernetesAPI: &cluster.KubernetesAPI{
			Port: "443",
		},
		Network: &cluster.Network{
			Type:       cluster.NetworkTypePublic,
			CIDR:       "10.0.0.0/16",
			InternetGW: &cluster.InternetGW{},
		},
		Values: &cluster.Values{
			ItemMap: map[string]string{
				"INJECTEDTOKEN": kubeadm.GetRandomToken(),
			},
		},
		ServerPools: []*cluster.ServerPool{
			{
				Type:     cluster.ServerPoolTypeMaster,
				Name:     fmt.Sprintf("%s.master", name),
				MaxCount: 1,
				MinCount: 1,
				Image:    "ami-835b4efa",
				Size:     "t2.xlarge",
				BootstrapScripts: []string{
					"bootstrap/amazon_k8s_ubuntu_16.04_master.sh",
				},
				InstanceProfile: &cluster.IAMInstanceProfile{
					Name: fmt.Sprintf("%s-KubicornMasterInstanceProfile", name),
					Role: &cluster.IAMRole{
						Name: fmt.Sprintf("%s-KubicornMasterRole", name),
						Policies: []*cluster.IAMPolicy{
							{
								Name: "MasterPolicy",
								Document: `{
								  "Version": "2012-10-17",
								  "Statement": [
									 {
										"Effect": "Allow",
										"Action": [
										   "ec2:*",
										   "elasticloadbalancing:*",
										   "ecr:GetAuthorizationToken",
										   "ecr:BatchCheckLayerAvailability",
										   "ecr:GetDownloadUrlForLayer",
										   "ecr:GetRepositoryPolicy",
										   "ecr:DescribeRepositories",
										   "ecr:ListImages",
										   "ecr:BatchGetImage",
										   "autoscaling:DescribeAutoScalingGroups",
										   "autoscaling:UpdateAutoScalingGroup"
										],
										"Resource": "*"
									 }
								  ]
								}`,
							},
						},
					},
				},
				Subnets: []*cluster.Subnet{
					{
						Name: fmt.Sprintf("%s.master", name),
						CIDR: "10.0.0.0/24",
						Zone: "us-west-2a",
					},
				},
				AwsConfiguration: &cluster.AwsConfiguration{},
				Firewalls: []*cluster.Firewall{
					{
						Name: fmt.Sprintf("%s.master-external-%s", name, uuid.TimeOrderedUUID()),
						IngressRules: []*cluster.IngressRule{
							{
								IngressFromPort: "22",
								IngressToPort:   "22",
								IngressSource:   "0.0.0.0/0",
								IngressProtocol: "tcp",
							},
							{
								IngressFromPort: "443",
								IngressToPort:   "443",
								IngressSource:   "0.0.0.0/0",
								IngressProtocol: "tcp",
							},
							{
								IngressFromPort: "0",
								IngressToPort:   "65535",
								IngressSource:   "10.0.100.0/24",
								IngressProtocol: "-1",
							},
						},
					},
				},
			},
			{
				Type:     cluster.ServerPoolTypeNode,
				Name:     fmt.Sprintf("%s.node", name),
				MaxCount: 1,
				MinCount: 1,
				Image:    "ami-835b4efa",
				Size:     "t2.medium",
				BootstrapScripts: []string{
					"bootstrap/amazon_k8s_ubuntu_16.04_node.sh",
				},
				InstanceProfile: &cluster.IAMInstanceProfile{
					Name: fmt.Sprintf("%s-KubicornNodeInstanceProfile", name),
					Role: &cluster.IAMRole{
						Name: fmt.Sprintf("%s-KubicornNodeRole", name),
						Policies: []*cluster.IAMPolicy{
							{
								Name: "NodePolicy",
								Document: `{
								  "Version": "2012-10-17",
								  "Statement": [
									 {
										"Effect": "Allow",
										"Action": [
										   "ec2:Describe*",
										   "ecr:GetAuthorizationToken",
										   "ecr:BatchCheckLayerAvailability",
										   "ecr:GetDownloadUrlForLayer",
										   "ecr:GetRepositoryPolicy",
										   "ecr:DescribeRepositories",
										   "ecr:ListImages",
										   "ecr:BatchGetImage",
										   "autoscaling:DescribeAutoScalingGroups",
										   "autoscaling:UpdateAutoScalingGroup"
										],
										"Resource": "*"
									 }
								  ]
								}`,
							},
						},
					},
				},
				Subnets: []*cluster.Subnet{
					{
						Name: fmt.Sprintf("%s.node", name),
						CIDR: "10.0.100.0/24",
						Zone: "us-west-2b",
					},
				},
				AwsConfiguration: &cluster.AwsConfiguration{},
				Firewalls: []*cluster.Firewall{
					{
						Name: fmt.Sprintf("%s.node-external-%s", name, uuid.TimeOrderedUUID()),
						IngressRules: []*cluster.IngressRule{
							{
								IngressFromPort: "22",
								IngressToPort:   "22",
								IngressSource:   "0.0.0.0/0",
								IngressProtocol: "tcp",
							},
							{
								IngressFromPort: "0",
								IngressToPort:   "65535",
								IngressSource:   "10.0.0.0/24",
								IngressProtocol: "-1",
							},
						},
					},
				},
			},
		},
	}
}
<|endoftext|>


<commit_before>package cipherfactory

import (
	"bytes"
	"crypto/rand"
	"io"
	"testing"
)

func TestFactoryCreation(t *testing.T) {

	f := Create()

	if f == nil {
		t.Fatal("Could not create default cipher factory")
	}

	if f.GetMinKeySourceBytes() < 16 {
		t.Fatal("Invalid minimal key size, 16 bytes (128 bits) is minimum to successfully protect cinode")
	}

}

func TestFactoryEncryptorCreation(t *testing.T) {

	f := Create()

	buff := &bytes.Buffer{}

	key := make([]byte, f.GetMinKeySourceBytes())
	iv := make([]byte, 0)

	enc, keyStr, err := f.CreateEncryptor(key, iv, buff)

	if err != nil {
		t.Fatalf("Couldn't create encryptor: %v", err)
	}

	if enc == nil {
		t.Fatal("Nil encoder received")
	}

	if keyStr == "" {
		t.Fatal("Nil key returned")
	}
}

func TestFactoryEncryptorCreationFailure(t *testing.T) {

	f := Create()

	buff := &bytes.Buffer{}

	key := make([]byte, f.GetMinKeySourceBytes())
	iv := make([]byte, 0)

	for _, l := range []int{0, 1, f.GetMinKeySourceBytes() - 1} {

		enc, keyStr, err := f.CreateEncryptor(key[:l], iv, buff)

		if err == nil {
			t.Fatal("Did create encryptor with insufficient key size")
		}

		if enc != nil {
			t.Fatal("Got encryptor although error reported")
		}

		if keyStr != "" {
			t.Fatal("Got key although error reported")
		}
	}
}

func TestFactoryEncryptorDecryptorPair(t *testing.T) {

	testSet := [][]byte{
		[]byte{},
		[]byte{47},
		[]byte{13, 17},
		[]byte{54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76},
		make([]byte, 1089),
	}

	// Last entry in the set will be set to random data
	rand.Read(testSet[len(testSet)-1])

	for _, testData := range testSet {

		f := Create()

		buff := &bytes.Buffer{}

		key := make([]byte, f.GetMinKeySourceBytes())
		iv := make([]byte, 0)

		enc, keyStr, err := f.CreateEncryptor(key, iv, buff)

		if err != nil {
			t.Fatalf("Error creating encryptor: %v", err)
		}

		n, err := enc.Write(testData)

		if err != nil {
			t.Fatalf("Error writing to encryptor: %v", err)
		}

		if n != len(testData) {
			t.Fatalf("Not enough data written to the encryptor, requested: %v, got %v", len(testData), n)
		}

		dec, err := f.CreateDecryptor(keyStr, iv, buff)

		if err != nil {
			t.Fatalf("Error creating decryptor: %v", err)
		}

		if dec == nil {
			t.Fatalf("Didn't get decryptor")
		}

		buff2 := &bytes.Buffer{}

		_, err = io.Copy(buff2, dec)
		if err != nil {
			t.Fatalf("Couldn't decode data: %v", err)
		}

		if bytes.Compare(buff2.Bytes(), testData) != 0 {
			t.Fatal("Decryptor returned invalid data")
		}
	}
}
<commit_msg>Add some tests for factory's hasher<commit_after>package cipherfactory

import (
	"bytes"
	"crypto/rand"
	"io"
	"testing"
)

func TestFactoryCreation(t *testing.T) {

	f := Create()

	if f == nil {
		t.Fatal("Could not create default cipher factory")
	}

	if f.GetMinKeySourceBytes() < 16 {
		t.Fatal("Invalid minimal key size, 16 bytes (128 bits) is minimum to successfully protect cinode")
	}

}

func TestFactoryEncryptorCreation(t *testing.T) {

	f := Create()

	buff := &bytes.Buffer{}

	key := make([]byte, f.GetMinKeySourceBytes())
	iv := make([]byte, 0)

	enc, keyStr, err := f.CreateEncryptor(key, iv, buff)

	if err != nil {
		t.Fatalf("Couldn't create encryptor: %v", err)
	}

	if enc == nil {
		t.Fatal("Nil encoder received")
	}

	if keyStr == "" {
		t.Fatal("Nil key returned")
	}
}

func TestFactoryEncryptorCreationFailure(t *testing.T) {

	f := Create()

	buff := &bytes.Buffer{}

	key := make([]byte, f.GetMinKeySourceBytes())
	iv := make([]byte, 0)

	for _, l := range []int{0, 1, f.GetMinKeySourceBytes() - 1} {

		enc, keyStr, err := f.CreateEncryptor(key[:l], iv, buff)

		if err == nil {
			t.Fatal("Did create encryptor with insufficient key size")
		}

		if enc != nil {
			t.Fatal("Got encryptor although error reported")
		}

		if keyStr != "" {
			t.Fatal("Got key although error reported")
		}
	}
}

func TestFactoryEncryptorDecryptorPair(t *testing.T) {

	testSet := [][]byte{
		[]byte{},
		[]byte{47},
		[]byte{13, 17},
		[]byte{54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76},
		make([]byte, 1089),
	}

	// Last entry in the set will be set to random data
	rand.Read(testSet[len(testSet)-1])

	for _, testData := range testSet {

		f := Create()

		buff := &bytes.Buffer{}

		key := make([]byte, f.GetMinKeySourceBytes())
		iv := make([]byte, 0)

		enc, keyStr, err := f.CreateEncryptor(key, iv, buff)

		if err != nil {
			t.Fatalf("Error creating encryptor: %v", err)
		}

		n, err := enc.Write(testData)

		if err != nil {
			t.Fatalf("Error writing to encryptor: %v", err)
		}

		if n != len(testData) {
			t.Fatalf("Not enough data written to the encryptor, requested: %v, got %v", len(testData), n)
		}

		dec, err := f.CreateDecryptor(keyStr, iv, buff)

		if err != nil {
			t.Fatalf("Error creating decryptor: %v", err)
		}

		if dec == nil {
			t.Fatalf("Didn't get decryptor")
		}

		buff2 := &bytes.Buffer{}

		_, err = io.Copy(buff2, dec)
		if err != nil {
			t.Fatalf("Couldn't decode data: %v", err)
		}

		if bytes.Compare(buff2.Bytes(), testData) != 0 {
			t.Fatal("Decryptor returned invalid data")
		}
	}
}

func TestFactoryHasher(t *testing.T) {

	f := Create()

	h, err := f.CreateHasher()
	if err != nil {
		t.Fatalf("Couldn't create hasher: %v", err)
	}
	if h == nil {
		t.Fatal("Did not get valid hasher")
	}

	h.Write([]byte{1, 2, 3, 4, 5, 6, 7, 8})
	hash := h.Sum(nil)

	if hash == nil {
		t.Fatalf("Invalid hasher: didn't create hash sum")
	}

	if len(hash) < 16 {
		t.Fatalf("Invalid size of generated hash, at least 16 bytes is required")
	}
}
<|endoftext|>


<commit_before>package core

import (
	"errors"
	"time"
)

// NewBlock creates a new block from a spec
func NewBlock(s Spec) *Block {
	var in []Input
	var out []Output

	for _, v := range s.Inputs {
		in = append(in, Input{
			Name:  v.Name,
			Type:  v.Type,
			Value: nil,
			C:     make(chan Message),
		})
	}

	for _, v := range s.Outputs {
		out = append(out, Output{
			Name:        v.Name,
			Type:        v.Type,
			Connections: make(map[Connection]struct{}),
		})
	}

	return &Block{
		state: BlockState{
			make(MessageMap),
			make(MessageMap),
			make(MessageMap),
			make(Manifest),
			false,
		},
		routing: BlockRouting{
			Inputs:        in,
			Outputs:       out,
			InterruptChan: make(chan Interrupt),
		},
		kernel:     s.Kernel,
		sourceType: s.Source,
		Monitor:    make(chan MonitorMessage, 1),
		lastCrank:  time.Now(),
		done:       make(chan struct{}),
	}
}

// suture: the main routine the block runs
func (b *Block) Serve() {
	defer func() {
		b.done <- struct{}{}
	}()
	for {
		var interrupt Interrupt

		b.routing.RLock()
		for {
			interrupt = b.receive()
			if interrupt != nil {
				break
			}

			interrupt = b.process()
			if interrupt != nil {
				break
			}

			interrupt = b.broadcast()
			if interrupt != nil {
				break
			}

			b.crank()
		}
		b.routing.RUnlock()
		b.routing.Lock()
		if ok := interrupt(); !ok {
			b.routing.Unlock()
			return
		}
		b.routing.Unlock()
	}
}

func (b *Block) exportInput(id RouteIndex) (*Input, error) {
	if int(id) >= len(b.routing.Inputs) || int(id) < 0 {
		return nil, errors.New("index out of range")
	}

	if b.routing.Inputs[id].Value == nil {
		return &b.routing.Inputs[id], nil
	}

	return &Input{
		Value: &InputValue{
			Data: Copy((*b.routing.Inputs[id].Value).Data),
		},
		C:    b.routing.Inputs[id].C,
		Name: b.routing.Inputs[id].Name,
	}, nil

}

// GetInput returns the specified Input
func (b *Block) GetInput(id RouteIndex) (Input, error) {
	b.routing.RLock()
	r, err := b.exportInput(id)
	if err != nil {
		b.routing.RUnlock()
		return Input{}, err
	}
	b.routing.RUnlock()
	return *r, err
}

// GetInputs returns all inputs for a block.
func (b *Block) GetInputs() []Input {
	b.routing.RLock()
	re := make([]Input, len(b.routing.Inputs), len(b.routing.Inputs))
	for i, _ := range b.routing.Inputs {
		r, _ := b.exportInput(RouteIndex(i))
		re[i] = *r
	}
	b.routing.RUnlock()
	return re
}

// RouteValue sets the route to always be the specified value
func (b *Block) SetInput(id RouteIndex, v *InputValue) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if int(id) < 0 || int(id) >= len(b.routing.Inputs) {
			returnVal <- errors.New("input out of range")
			return true
		}

		b.routing.Inputs[id].Value = v

		returnVal <- nil
		return true
	}
	return <-returnVal
}

// Outputs return a list of manifest pairs for the block
func (b *Block) GetOutputs() []Output {
	b.routing.RLock()
	m := make([]Output, len(b.routing.Outputs), len(b.routing.Outputs))
	for id, out := range b.routing.Outputs {
		m[id] = Output{
			Name:        out.Name,
			Type:        out.Type,
			Connections: make(map[Connection]struct{}),
		}
		for k, _ := range out.Connections {
			m[id].Connections[k] = struct{}{}
		}
	}
	b.routing.RUnlock()
	return m
}

func (b *Block) GetSource() Source {
	b.routing.RLock()
	v := b.routing.Source
	b.routing.RUnlock()
	return v
}

// sets a store for the block. can be set to nil
func (b *Block) SetSource(s Source) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if s != nil && s.GetType() != b.sourceType {
			returnVal <- errors.New("invalid source type for this block")
			return true
		}
		b.routing.Source = s
		returnVal <- nil
		return true
	}
	return <-returnVal
}

// Connect connects a Route, specified by ID, to a connection
func (b *Block) Connect(id RouteIndex, c Connection) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if int(id) < 0 || int(id) >= len(b.routing.Outputs) {
			returnVal <- errors.New("output out of range")
			return true
		}

		if _, ok := b.routing.Outputs[id].Connections[c]; ok {
			returnVal <- errors.New("this connection already exists on this output")
			return true
		}

		b.routing.Outputs[id].Connections[c] = struct{}{}
		returnVal <- nil
		return true
	}
	return <-returnVal
}

// Disconnect removes a connection from a Input
func (b *Block) Disconnect(id RouteIndex, c Connection) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if int(id) < 0 || int(id) >= len(b.routing.Outputs) {
			returnVal <- errors.New("output out of range")
			return true
		}

		if _, ok := b.routing.Outputs[id].Connections[c]; !ok {
			returnVal <- errors.New("connection does not exist")
			return true
		}

		delete(b.routing.Outputs[id].Connections, c)
		returnVal <- nil
		return true
	}
	return <-returnVal
}

func (b *Block) Reset() {
	b.crank()

	// reset block's state as well. currently this only applies to a handful of
	// blocks, like GET and first.
	for k, _ := range b.state.internalValues {
		delete(b.state.internalValues, k)
	}

	return
}

func (b *Block) Stop() {
	b.routing.InterruptChan <- func() bool {
		return false
	}
	<-b.done
	return
}

// wait and listen for all kernel inputs to be filled.
func (b *Block) receive() Interrupt {
	for id, input := range b.routing.Inputs {
		b.Monitor <- MonitorMessage{
			BI_RECEIVE,
			id,
		}

		//if we have already received a value on this input, skip.
		if _, ok := b.state.inputValues[RouteIndex(id)]; ok {
			continue
		}

		if input.Value != nil {
			b.state.inputValues[RouteIndex(id)] = Copy(input.Value.Data)
			continue
		}

		select {
		case m := <-input.C:
			b.state.inputValues[RouteIndex(id)] = m
		case f := <-b.routing.InterruptChan:
			return f
		}
	}
	return nil
}

// run kernel on inputs, produce outputs
func (b *Block) process() Interrupt {
	b.Monitor <- MonitorMessage{
		BI_KERNEL,
		nil,
	}

	if b.state.Processed == true {
		return nil
	}

	// if this kernel relies on an external shared state then we need to
	// block until an interrupt connects us to a shared external state.

	if b.sourceType != NONE && b.routing.Source == nil {
		select {
		case f := <-b.routing.InterruptChan:
			return f
		}
	}

	// we should only be able to get here if
	// - we don't need an shared state
	// - we have an external shared state and it has been attached

	// TODO there is a potential generalisation here for sources that don't need to be locked
	if b.sourceType != NONE && b.sourceType != SERVER {
		b.routing.Source.Lock()
	}

	// run the kernel
	interrupt := b.kernel(b.state.inputValues,
		b.state.outputValues,
		b.state.internalValues,
		b.routing.Source,
		b.routing.InterruptChan)

	if interrupt != nil {
		if b.sourceType != NONE && b.sourceType != SERVER {
			b.routing.Source.Unlock()
		}
		return interrupt
	}

	if b.sourceType != NONE && b.sourceType != SERVER {
		b.routing.Source.Unlock()
	}

	b.state.Processed = true

	return nil
}

func (b *Block) deliver(ensure bool) (bool, Interrupt) {
	// tally how many deliveries that we need to make in total. due to the fact
	// that the kernel _does not need_ to satisfy all outputs per crank, we need
	// to check to see if there are any messages on a given output before adding
	// it to the tally.
	// TODO: this can be possibly further optimized
	// - by caching the total connections
	// - by moving both the len(manifest) validation and tallying to the
	//   broadcast() func
	// - possibly convert manifest to a simple count instead a map
	total := 0
	for id, out := range b.routing.Outputs {
		if _, ok := b.state.outputValues[RouteIndex(id)]; ok {
			total += len(out.Connections)
		}
	}

	for id, out := range b.routing.Outputs {
		b.Monitor <- MonitorMessage{
			BI_BROADCAST,
			id,
		}

		// if the output key is not present in the output map, then we
		// don't deliver any message
		_, ok := b.state.outputValues[RouteIndex(id)]
		if !ok {
			continue
		}

		// if there no connection for this output then wait until there
		// is one. that means we have to wait for an interrupt.
		if len(out.Connections) == 0 {
			select {
			case f := <-b.routing.InterruptChan:
				return len(b.state.manifest) == total, f
			}
		}
		for c, _ := range out.Connections {
			// check to see if we have delivered a message to this
			// connection for this block crank. if we have, then
			// skip this delivery.
			m := ManifestPair{id, c}
			if _, ok := b.state.manifest[m]; ok {
				continue
			}

			// ensure is a flag that toggles between a blocking send and a non-
			// blocking send. in some circumstances, connections may get
			// "tangled". When this happens, a connection may block the send
			// for another connection on the same broadcast pin. This can
			// happen when a single broadcast pin may attempt to deliver to two
			// separate inputs on a single block. Because a block receives in
			// order, the broadcasting pin may attempt to send to a pin that is
			// not currently in a receive state. This results in eternal
			// blocking.
			if ensure {
				select {
				case c <- b.state.outputValues[RouteIndex(id)]:
					// set that we have delivered the message.
					b.state.manifest[m] = struct{}{}
				case f := <-b.routing.InterruptChan:
					return len(b.state.manifest) == total, f
				}
			} else {
				select {
				case c <- b.state.outputValues[RouteIndex(id)]:
					// set that we have delivered the message.
					b.state.manifest[m] = struct{}{}
				default:
				}
			}
		}
	}
	return len(b.state.manifest) == total, nil
}

// broadcast the kernel output to all connections on all outputs.
func (b *Block) broadcast() Interrupt {
	// we attempt to deliver twice. the first with a non-blocking send, and
	// secondly, a blocking send. If the non-blocking send fails at least once,
	// we revert to a blocking send state.
	done, i := b.deliver(false)
	if i != nil {
		return i
	}
	if done {
		return nil
	}
	done, i = b.deliver(true)
	if i != nil {
		return i
	}
	if !done {
		panic("cataclysmic error, we should never get here")
	}

	return nil
}

// cleanup all block state for this crank of the block
func (b *Block) crank() {
	for k, _ := range b.state.inputValues {
		delete(b.state.inputValues, k)
	}
	for k, _ := range b.state.outputValues {
		delete(b.state.outputValues, k)
	}
	for k, _ := range b.state.manifest {
		delete(b.state.manifest, k)
	}
	b.state.Processed = false
}
<commit_msg>fixing blocking send for sequential broadcast/recieve in core/block.go<commit_after>package core

import (
	"errors"
	"time"
)

// NewBlock creates a new block from a spec
func NewBlock(s Spec) *Block {
	var in []Input
	var out []Output

	for _, v := range s.Inputs {
		in = append(in, Input{
			Name:  v.Name,
			Type:  v.Type,
			Value: nil,
			C:     make(chan Message, 1),
		})
	}

	for _, v := range s.Outputs {
		out = append(out, Output{
			Name:        v.Name,
			Type:        v.Type,
			Connections: make(map[Connection]struct{}),
		})
	}

	return &Block{
		state: BlockState{
			make(MessageMap),
			make(MessageMap),
			make(MessageMap),
			make(Manifest),
			false,
		},
		routing: BlockRouting{
			Inputs:        in,
			Outputs:       out,
			InterruptChan: make(chan Interrupt),
		},
		kernel:     s.Kernel,
		sourceType: s.Source,
		Monitor:    make(chan MonitorMessage, 1),
		lastCrank:  time.Now(),
		done:       make(chan struct{}),
	}
}

// suture: the main routine the block runs
func (b *Block) Serve() {
	defer func() {
		b.done <- struct{}{}
	}()
	for {
		var interrupt Interrupt

		b.routing.RLock()
		for {
			interrupt = b.receive()
			if interrupt != nil {
				break
			}

			interrupt = b.process()
			if interrupt != nil {
				break
			}

			interrupt = b.broadcast()
			if interrupt != nil {
				break
			}

			b.crank()
		}
		b.routing.RUnlock()
		b.routing.Lock()
		if ok := interrupt(); !ok {
			b.routing.Unlock()
			return
		}
		b.routing.Unlock()
	}
}

func (b *Block) exportInput(id RouteIndex) (*Input, error) {
	if int(id) >= len(b.routing.Inputs) || int(id) < 0 {
		return nil, errors.New("index out of range")
	}

	if b.routing.Inputs[id].Value == nil {
		return &b.routing.Inputs[id], nil
	}

	return &Input{
		Value: &InputValue{
			Data: Copy((*b.routing.Inputs[id].Value).Data),
		},
		C:    b.routing.Inputs[id].C,
		Name: b.routing.Inputs[id].Name,
	}, nil

}

// GetInput returns the specified Input
func (b *Block) GetInput(id RouteIndex) (Input, error) {
	b.routing.RLock()
	r, err := b.exportInput(id)
	if err != nil {
		b.routing.RUnlock()
		return Input{}, err
	}
	b.routing.RUnlock()
	return *r, err
}

// GetInputs returns all inputs for a block.
func (b *Block) GetInputs() []Input {
	b.routing.RLock()
	re := make([]Input, len(b.routing.Inputs), len(b.routing.Inputs))
	for i, _ := range b.routing.Inputs {
		r, _ := b.exportInput(RouteIndex(i))
		re[i] = *r
	}
	b.routing.RUnlock()
	return re
}

// RouteValue sets the route to always be the specified value
func (b *Block) SetInput(id RouteIndex, v *InputValue) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if int(id) < 0 || int(id) >= len(b.routing.Inputs) {
			returnVal <- errors.New("input out of range")
			return true
		}

		b.routing.Inputs[id].Value = v

		returnVal <- nil
		return true
	}
	return <-returnVal
}

// Outputs return a list of manifest pairs for the block
func (b *Block) GetOutputs() []Output {
	b.routing.RLock()
	m := make([]Output, len(b.routing.Outputs), len(b.routing.Outputs))
	for id, out := range b.routing.Outputs {
		m[id] = Output{
			Name:        out.Name,
			Type:        out.Type,
			Connections: make(map[Connection]struct{}),
		}
		for k, _ := range out.Connections {
			m[id].Connections[k] = struct{}{}
		}
	}
	b.routing.RUnlock()
	return m
}

func (b *Block) GetSource() Source {
	b.routing.RLock()
	v := b.routing.Source
	b.routing.RUnlock()
	return v
}

// sets a store for the block. can be set to nil
func (b *Block) SetSource(s Source) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if s != nil && s.GetType() != b.sourceType {
			returnVal <- errors.New("invalid source type for this block")
			return true
		}
		b.routing.Source = s
		returnVal <- nil
		return true
	}
	return <-returnVal
}

// Connect connects a Route, specified by ID, to a connection
func (b *Block) Connect(id RouteIndex, c Connection) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if int(id) < 0 || int(id) >= len(b.routing.Outputs) {
			returnVal <- errors.New("output out of range")
			return true
		}

		if _, ok := b.routing.Outputs[id].Connections[c]; ok {
			returnVal <- errors.New("this connection already exists on this output")
			return true
		}

		b.routing.Outputs[id].Connections[c] = struct{}{}
		returnVal <- nil
		return true
	}
	return <-returnVal
}

// Disconnect removes a connection from a Input
func (b *Block) Disconnect(id RouteIndex, c Connection) error {
	returnVal := make(chan error, 1)
	b.routing.InterruptChan <- func() bool {
		if int(id) < 0 || int(id) >= len(b.routing.Outputs) {
			returnVal <- errors.New("output out of range")
			return true
		}

		if _, ok := b.routing.Outputs[id].Connections[c]; !ok {
			returnVal <- errors.New("connection does not exist")
			return true
		}

		delete(b.routing.Outputs[id].Connections, c)
		returnVal <- nil
		return true
	}
	return <-returnVal
}

func (b *Block) Reset() {
	b.crank()

	// reset block's state as well. currently this only applies to a handful of
	// blocks, like GET and first.
	for k, _ := range b.state.internalValues {
		delete(b.state.internalValues, k)
	}

	// if there are any messages on the input channels, flush them.
	// note: all blocks that are sending to this block MUST BE IN A
	// STOPPED STATE. if any block routines that posess this block's
	// input channel are in a RUNNING state, this flush will not work
	// because it will simply pull another message into the buffer.
	for _, input := range b.routing.Inputs {
		select {
		case <-input.C:
		default:
		}
	}

	return
}

func (b *Block) Stop() {
	b.routing.InterruptChan <- func() bool {
		return false
	}
	<-b.done
	return
}

// wait and listen for all kernel inputs to be filled.
func (b *Block) receive() Interrupt {
	for id, input := range b.routing.Inputs {
		b.Monitor <- MonitorMessage{
			BI_RECEIVE,
			id,
		}

		//if we have already received a value on this input, skip.
		if _, ok := b.state.inputValues[RouteIndex(id)]; ok {
			continue
		}

		if input.Value != nil {
			b.state.inputValues[RouteIndex(id)] = Copy(input.Value.Data)
			continue
		}

		select {
		case m := <-input.C:
			b.state.inputValues[RouteIndex(id)] = m
		case f := <-b.routing.InterruptChan:
			return f
		}
	}
	return nil
}

// run kernel on inputs, produce outputs
func (b *Block) process() Interrupt {
	b.Monitor <- MonitorMessage{
		BI_KERNEL,
		nil,
	}

	if b.state.Processed == true {
		return nil
	}

	// if this kernel relies on an external shared state then we need to
	// block until an interrupt connects us to a shared external state.

	if b.sourceType != NONE && b.routing.Source == nil {
		select {
		case f := <-b.routing.InterruptChan:
			return f
		}
	}

	// we should only be able to get here if
	// - we don't need an shared state
	// - we have an external shared state and it has been attached

	// TODO there is a potential generalisation here for sources that don't need to be locked
	if b.sourceType != NONE && b.sourceType != SERVER {
		b.routing.Source.Lock()
	}

	// run the kernel
	interrupt := b.kernel(b.state.inputValues,
		b.state.outputValues,
		b.state.internalValues,
		b.routing.Source,
		b.routing.InterruptChan)

	if interrupt != nil {
		if b.sourceType != NONE && b.sourceType != SERVER {
			b.routing.Source.Unlock()
		}
		return interrupt
	}

	if b.sourceType != NONE && b.sourceType != SERVER {
		b.routing.Source.Unlock()
	}

	b.state.Processed = true

	return nil
}

// broadcast the kernel output to all connections on all outputs.
func (b *Block) broadcast() Interrupt {
	for id, out := range b.routing.Outputs {
		select {
		case b.Monitor <- MonitorMessage{
			BI_BROADCAST,
			id,
			//			time.Now(),
		}:
		default:
		}

		// if the output key is not present in the output map, then we
		// don't deliver any message
		_, ok := b.state.outputValues[RouteIndex(id)]
		if !ok {
			continue
		}

		// if there no connection for this output then wait until there
		// is one. that means we have to wait for an interrupt.
		if len(out.Connections) == 0 {
			select {
			case f := <-b.routing.InterruptChan:
				return f
			}
		}
		for c, _ := range out.Connections {
			// check to see if we have delivered a message to this
			// connection for this block crank. if we have, then
			// skip this delivery.
			m := ManifestPair{id, c}
			if _, ok := b.state.manifest[m]; ok {
				continue
			}

			select {
			case c <- b.state.outputValues[RouteIndex(id)]:
				// set that we have delivered the message.
				b.state.manifest[m] = struct{}{}
			case f := <-b.routing.InterruptChan:
				return f
			}
		}
	}
	return nil
}

// cleanup all block state for this crank of the block
func (b *Block) crank() {
	for k, _ := range b.state.inputValues {
		delete(b.state.inputValues, k)
	}
	for k, _ := range b.state.outputValues {
		delete(b.state.outputValues, k)
	}
	for k, _ := range b.state.manifest {
		delete(b.state.manifest, k)
	}
	b.state.Processed = false
}
<|endoftext|>


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

package py

// #include "utils.h"
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
import "C"

import (
	"fmt"
	"os"
	"unsafe"
)

type BaseObject struct {
	AbstractObject
	C.PyObject
}

var BaseType = (*Type)(unsafe.Pointer(&C.PyBaseObject_Type))

func newBaseObject(obj *C.PyObject) *BaseObject {
	return (*BaseObject)(unsafe.Pointer(obj))
}

func (obj *BaseObject) Call(args *Tuple, kwds *Dict) (Object, os.Error) {
	ret := C.PyObject_Call(c(obj), c(args), c(kwds))
	return obj2ObjErr(ret)
}

func (obj *BaseObject) CallObject(args *Tuple) (Object, os.Error) {
	var a *C.PyObject = nil
	if args != nil {
		a = c(args)
	}
	ret := C.PyObject_CallObject(c(obj), a)
	return obj2ObjErr(ret)
}

func (obj *BaseObject) CallFunction(format string, args ...interface{}) (Object, os.Error) {
	t, err := buildTuple(format, args...)
	if err != nil {
		return nil, err
	}
	return obj.CallObject(t)
}

func (obj *BaseObject) CallMethod(name string, format string, args ...interface{}) (Object, os.Error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	f := C.PyObject_GetAttrString(c(obj), cname)
	if f == nil {
		return nil, fmt.Errorf("AttributeError: %s", name)
	}
	defer C.decref(f)

	if C.PyCallable_Check(f) == 0 {
		return nil, fmt.Errorf("TypeError: attribute of type '%s' is not callable", name)
	}

	t, err := buildTuple(format, args...)
	if err != nil {
		return nil, err
	}

	ret := C.PyObject_CallObject(f, c(t))
	return obj2ObjErr(ret)
}

func (obj *BaseObject) CallFunctionObjArgs(args ...Object) (Object, os.Error) {
	t, err := PackTuple(args...)
	if err != nil {
		return nil, err
	}
	return obj.CallObject(t)
}

func (obj *BaseObject) CallMethodObjArgs(name string, args ...Object) (Object, os.Error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	f := C.PyObject_GetAttrString(c(obj), cname)
	if f == nil {
		return nil, fmt.Errorf("AttributeError: %s", name)
	}
	defer C.decref(f)

	if C.PyCallable_Check(f) == 0 {
		return nil, fmt.Errorf("TypeError: attribute of type '%s' is not callable", name)
	}

	t, err := PackTuple(args...)
	if err != nil {
		return nil, err
	}

	ret := C.PyObject_CallObject(f, c(t))
	return obj2ObjErr(ret)
}

func (obj *BaseObject) Dir() (Object, os.Error) {
	ret := C.PyObject_Dir(c(obj))
	return obj2ObjErr(ret)
}
<commit_msg>Implement most of the PyObject_ functions<commit_after>// Copyright 2011 Julian Phillips.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package py

// #include "utils.h"
// static inline void decref(PyObject *obj) { Py_DECREF(obj); }
import "C"

import (
	"fmt"
	"os"
	"unsafe"
)

type BaseObject struct {
	AbstractObject
	C.PyObject
}

var BaseType = (*Type)(unsafe.Pointer(&C.PyBaseObject_Type))

func newBaseObject(obj *C.PyObject) *BaseObject {
	return (*BaseObject)(unsafe.Pointer(obj))
}

// HasAttr returns true if "obj" has the attribute "name".  This is equivalent
// to the Python "hasattr(obj, name)".
func (obj *BaseObject) HasAttr(name Object) bool {
	ret := C.PyObject_HasAttr(c(obj), c(name))
	if ret == 1 {
		return true
	}
	return false
}

// HasAttrString returns true if "obj" has the attribute "name".  This is
// equivalent to the Python "hasattr(obj, name)".
func (obj *BaseObject) HasAttrString(name string) bool {
	s := C.CString(name)
	defer C.free(unsafe.Pointer(s))
	ret := C.PyObject_HasAttrString(c(obj), s)
	if ret == 1 {
		return true
	}
	return false
}

// GetAttr returns the attribute of "obj" with the name "name".  This is
// equivalent to the Python "obj.name".
//
// Return value: New Reference.
func (obj *BaseObject) GetAttr(name Object) (Object, os.Error) {
	ret := C.PyObject_GetAttr(c(obj), c(name))
	return obj2ObjErr(ret)
}

// GetAttrString returns the attribute of "obj" with the name "name".  This is
// equivalent to the Python "obj.name".
//
// Return value: New Reference.
func (obj *BaseObject) GetAttrString(name string) (Object, os.Error) {
	s := C.CString(name)
	defer C.free(unsafe.Pointer(s))
	ret := C.PyObject_GetAttrString(c(obj), s)
	return obj2ObjErr(ret)
}

// PyObject_GenericGetAttr : This is an internal helper function - we shouldn't
// need to expose it ...

// SetAttr sets the attribute of "obj" with the name "name" to "value".  This is
// equivalent to the Python "obj.name = value".
func (obj *BaseObject) SetAttr(name, value Object) os.Error {
	ret := C.PyObject_SetAttr(c(obj), c(name), c(value))
	return int2Err(ret)
}

// SetAttrString sets the attribute of "obj" with the name "name" to "value".
// This is equivalent to the Python "obj.name = value".
func (obj *BaseObject) SetAttrString(name string, value Object) os.Error {
	s := C.CString(name)
	defer C.free(unsafe.Pointer(s))
	ret := C.PyObject_SetAttrString(c(obj), s, c(value))
	return int2Err(ret)
}

// PyObject_GenericSetAttr : This is an internal helper function - we shouldn't
// need to expose it ...

// DelAttr deletes the attribute with the name "name" from "obj".  This is
// equivalent to the Python "del obj.name".
func (obj *BaseObject) DelAttr(name Object) os.Error {
	ret := C.PyObject_SetAttr(c(obj), c(name), nil)
	return int2Err(ret)
}

// DelAttrString deletes the attribute with the name "name" from "obj".  This is
// equivalent to the Python "del obj.name".
func (obj *BaseObject) DelAttrString(name string) os.Error {
	s := C.CString(name)
	defer C.free(unsafe.Pointer(s))
	ret := C.PyObject_SetAttrString(c(obj), s, nil)
	return int2Err(ret)
}

// RichCompare compares "obj" with "obj2" using the specified operation (LE, GE
// etc.), and returns the result.  The equivalent Python is "obj op obj2", where
// op is the corresponding Python operator for op.
//
// Return value: New Reference.
func (obj *BaseObject) RichCompare(obj2 Object, op Op) (Object, os.Error) {
	ret := C.PyObject_RichCompare(c(obj), c(obj2), C.int(op))
	return obj2ObjErr(ret)
}

// RichCompare compares "obj" with "obj2" using the specified operation (LE, GE
// etc.), and returns true or false.  The equivalent Python is "obj op obj2",
// where op is the corresponding Python operator for op.
func (obj *BaseObject) RichCompareBool(obj2 Object, op Op) (bool, os.Error) {
	ret := C.PyObject_RichCompareBool(c(obj), c(obj2), C.int(op))
	return int2BoolErr(ret)
}

// PyObject_Cmp : Thanks to multiple return values, we don't need this function
// to be available in Go.

// Compare returns the result of comparing "obj" and "obj2".  This is equivalent
// to the Python "cmp(obj, obj2)".
func (obj *BaseObject) Compare(obj2 Object) (int, os.Error) {
	ret := C.PyObject_Compare(c(obj), c(obj2))
	return int(ret), exception()
}

// Repr returns a String representation of "obj".  This is equivalent to the
// Python "repr(obj)".
//
// Return value: New Reference.
func (obj *BaseObject) Repr() (Object, os.Error) {
	ret := C.PyObject_Repr(c(obj))
	return obj2ObjErr(ret)
}

// Str returns a String representation of "obj".  This is equivalent to the
// Python "str(obj)".
//
// Return value: New Reference.
func (obj *BaseObject) Str() (Object, os.Error) {
	ret := C.PyObject_Str(c(obj))
	return obj2ObjErr(ret)
}

// Bytes returns a Bytes representation of "obj".  This is equivalent to the
// Python "bytes(obj)".  In Python 2.x this method is identical to Str().
//
// Return value: New Reference.
func (obj *BaseObject) Bytes() (Object, os.Error) {
	ret := C.PyObject_Bytes(c(obj))
	return obj2ObjErr(ret)
}

// PyObject_Unicode : TODO

// IsInstance returns true if "obj" is an instance of "cls", false otherwise.
// If "cls" is a Type instead of a class, then true will be return if "obj" is
// of that type.  If "cls" is a Tuple then true will be returned if "obj" is an
// instance of any of the Objects in the tuple.  This is equivalent to the
// Python "isinstance(obj, cls)".
func (obj *BaseObject) IsInstance(cls Object) (bool, os.Error) {
	ret := C.PyObject_IsInstance(c(obj), c(cls))
	return int2BoolErr(ret)
}

// IsSubclass retuns true if "obj" is a Subclass of "cls", false otherwise.  If
// "cls" is a Tuple, then true is returned if "obj" is a Subclass of any member
// of "cls".  This is equivalent to the Python "issubclass(obj, cls)".
func (obj *BaseObject) IsSubclass(cls Object) (bool, os.Error) {
	ret := C.PyObject_IsSubclass(c(obj), c(cls))
	return int2BoolErr(ret)
}

func (obj *BaseObject) Call(args *Tuple, kwds *Dict) (Object, os.Error) {
	ret := C.PyObject_Call(c(obj), c(args), c(kwds))
	return obj2ObjErr(ret)
}

func (obj *BaseObject) CallObject(args *Tuple) (Object, os.Error) {
	var a *C.PyObject = nil
	if args != nil {
		a = c(args)
	}
	ret := C.PyObject_CallObject(c(obj), a)
	return obj2ObjErr(ret)
}

func (obj *BaseObject) CallFunction(format string, args ...interface{}) (Object, os.Error) {
	t, err := buildTuple(format, args...)
	if err != nil {
		return nil, err
	}
	return obj.CallObject(t)
}

func (obj *BaseObject) CallMethod(name string, format string, args ...interface{}) (Object, os.Error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	f := C.PyObject_GetAttrString(c(obj), cname)
	if f == nil {
		return nil, fmt.Errorf("AttributeError: %s", name)
	}
	defer C.decref(f)

	if C.PyCallable_Check(f) == 0 {
		return nil, fmt.Errorf("TypeError: attribute of type '%s' is not callable", name)
	}

	t, err := buildTuple(format, args...)
	if err != nil {
		return nil, err
	}

	ret := C.PyObject_CallObject(f, c(t))
	return obj2ObjErr(ret)
}

func (obj *BaseObject) CallFunctionObjArgs(args ...Object) (Object, os.Error) {
	t, err := PackTuple(args...)
	if err != nil {
		return nil, err
	}
	return obj.CallObject(t)
}

func (obj *BaseObject) CallMethodObjArgs(name string, args ...Object) (Object, os.Error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	f := C.PyObject_GetAttrString(c(obj), cname)
	if f == nil {
		return nil, fmt.Errorf("AttributeError: %s", name)
	}
	defer C.decref(f)

	if C.PyCallable_Check(f) == 0 {
		return nil, fmt.Errorf("TypeError: attribute of type '%s' is not callable", name)
	}

	t, err := PackTuple(args...)
	if err != nil {
		return nil, err
	}

	ret := C.PyObject_CallObject(f, c(t))
	return obj2ObjErr(ret)
}

// PyObject_Hash : TODO

// PyObject_HashNotImplement : This is an internal function, that we probably
// don't need to export.

// PyObject_IsTrue : Implemented on AbstractObject

// PyObject_Not : Implemented on AbstractObject

// PyObject_Type : Implemented on AbstractObject

// PyObject_TypeCheck : TODO

// Length returns the length of the Object.  This is equivalent to the Python
// "len(obj)".
func (obj *BaseObject) Length() (int64, os.Error) {
	ret := C.PyObject_Length(c(obj))
	return int64(ret), exception()
}

// Size returns the length of the Object.  This is equivalent to the Python
// "len(obj)".
func (obj *BaseObject) Size() (int64, os.Error) {
	ret := C.PyObject_Size(c(obj))
	return int64(ret), exception()
}

// GetItem returns the element of "obj" corresponding to "key".  This is
// equivalent to the Python "obj[key]".
//
// Return value: New Reference.
func (obj *BaseObject) GetItem(key Object) (Object, os.Error) {
	ret := C.PyObject_GetItem(c(obj), c(key))
	return obj2ObjErr(ret)
}

// SetItem sets the element of "obj" corresponding to "key" to "value".  This is
// equivalent to the Python "obj[key] = value".
func (obj *BaseObject) SetItem(key, value Object) os.Error {
	ret := C.PyObject_SetItem(c(obj), c(key), c(value))
	return int2Err(ret)
}

// DelItem deletes the element from "obj" that corresponds to "key".  This is
// equivalent to the Python "del obj[key]".
func (obj *BaseObject) DelItem(key Object) os.Error {
	ret := C.PyObject_DelItem(c(obj), c(key))
	return int2Err(ret)
}

// PyObject_AsFileDescriptor : TODO

func (obj *BaseObject) Dir() (Object, os.Error) {
	ret := C.PyObject_Dir(c(obj))
	return obj2ObjErr(ret)
}

// PyObject_GetIter : TODO
<|endoftext|>


<commit_before><commit_msg>Fix lookups to use correct calls<commit_after><|endoftext|>


<commit_before>// Copyright 2013 tsuru 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 docker

import (
	"github.com/globocom/commandmocker"
	"github.com/globocom/config"
	"github.com/globocom/tsuru/fs/testing"
	"io/ioutil"
	"launchpad.net/gocheck"
	"os"
)

func (s *S) TestLXCCreate(c *gocheck.C) {
	config.Set("docker:authorized-key-path", "somepath")
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.create()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker run -d base /bin/bash container somepath"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestLXCStart(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.start()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker start container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestLXCStop(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.stop()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker stop container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestLXCDestroy(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.destroy()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker rm container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestContainerIP(c *gocheck.C) {
	config.Set("docker:ip-timeout", 10)
	file, _ := os.Open("testdata/dnsmasq.leases")
	data, err := ioutil.ReadAll(file)
	c.Assert(err, gocheck.IsNil)
	rfs := &testing.RecordingFs{FileContent: string(data)}
	fsystem = rfs
	defer func() {
		fsystem = nil
	}()
	f, _ := rfs.Open("/var/lib/misc/dnsmasq.leases")
	f.Write(data)
	f.Close()
	cont := container{name: "vm1"}
	c.Assert(cont.ip(), gocheck.Equals, "10.10.10.10")
	cont = container{name: "notfound"}
	c.Assert(cont.ip(), gocheck.Equals, "")
}
<commit_msg>provison/docker: fixed some tests.<commit_after>// Copyright 2013 tsuru 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 docker

import (
	"github.com/globocom/commandmocker"
	"github.com/globocom/config"
	"github.com/globocom/tsuru/fs/testing"
	"io/ioutil"
	"launchpad.net/gocheck"
	"os"
)

func (s *S) TestLXCCreate(c *gocheck.C) {
	config.Set("docker:authorized-key-path", "somepath")
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err, _ = container.create()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker run -d base /bin/bash container somepath"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestLXCStart(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.start()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker start container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestLXCStop(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.stop()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker stop container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestLXCDestroy(c *gocheck.C) {
	tmpdir, err := commandmocker.Add("sudo", "$*")
	c.Assert(err, gocheck.IsNil)
	defer commandmocker.Remove(tmpdir)
	container := container{name: "container"}
	err = container.destroy()
	c.Assert(err, gocheck.IsNil)
	c.Assert(commandmocker.Ran(tmpdir), gocheck.Equals, true)
	expected := "docker rm container"
	c.Assert(commandmocker.Output(tmpdir), gocheck.Equals, expected)
}

func (s *S) TestContainerIP(c *gocheck.C) {
	config.Set("docker:ip-timeout", 10)
	file, _ := os.Open("testdata/dnsmasq.leases")
	data, err := ioutil.ReadAll(file)
	c.Assert(err, gocheck.IsNil)
	rfs := &testing.RecordingFs{FileContent: string(data)}
	fsystem = rfs
	defer func() {
		fsystem = nil
	}()
	f, _ := rfs.Open("/var/lib/misc/dnsmasq.leases")
	f.Write(data)
	f.Close()
	cont := container{name: "vm1"}
	err, ip := cont.ip()
	c.Assert(err, gocheck.IsNil)
	c.Assert(ip, gocheck.Equals, "10.10.10.10")
	cont = container{name: "notfound"}
	err, ip = cont.ip()
	c.Assert(err, gocheck.IsNil)
	c.Assert(ip, gocheck.Equals, "")
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"io/ioutil"
	"os"

	"github.com/tent/tent-client-go"
)

var meta *tent.MetaPost

func discover() []*request {
	var err error
	meta, err = tent.Discover(os.Args[1])
	if err != nil {
		panic(err)
	}
	return getRequests()
}

func main() {
	examples := make(map[string]*request)
	tent.HTTP.Transport = &roundTripRecorder{roundTripper: tent.HTTP.Transport}

	discoveryReqs := discover()
	examples["discover_head"] = discoveryReqs[0]
	examples["discover_meta"] = discoveryReqs[1]

	res := make(map[string]string)
	for k, v := range examples {
		res[k] = requestMarkdown(v)
	}

	data, _ := json.Marshal(res)
	ioutil.WriteFile(os.Args[2], data, 0644)
}
<commit_msg>Add basic app create example<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/tent/tent-client-go"
)

var meta *tent.MetaPost
var client *tent.Client

func discover() []*request {
	var err error
	meta, err = tent.Discover(os.Args[1])
	maybePanic(err)
	client = &tent.Client{Servers: meta.Servers}
	return getRequests()
}

func createApp() []*request {
	post := tent.NewAppPost(&tent.App{
		Name: "Example App",
		URL:  "https://app.example.com",
		PostTypes: tent.AppPostTypes{
			Write: []string{"https://tent.io/types/post/v0"},
			Read:  []string{"https://tent.io/types/app/v0"},
		},
		RedirectURI: "https://app.example.com/oauth",
	})
	err := client.CreatePost(post)
	maybePanic(err)
	_, err = post.GetCredentials()
	maybePanic(err)
	return getRequests()
}

func main() {
	examples := make(map[string]*request)
	tent.HTTP.Transport = &roundTripRecorder{roundTripper: tent.HTTP.Transport}

	discoveryReqs := discover()
	examples["discover_head"] = discoveryReqs[0]
	examples["discover_meta"] = discoveryReqs[1]

	appReqs := createApp()
	examples["app_create"] = appReqs[0]
	examples["app_credentials"] = appReqs[1]

	res := make(map[string]string)
	for k, v := range examples {
		res[k] = requestMarkdown(v)
	}

	data, _ := json.Marshal(res)
	ioutil.WriteFile(os.Args[2], data, 0644)
}

func maybePanic(err error) {
	if err != nil {
		if resErr, ok := err.(*tent.BadResponseError); ok && resErr.TentError != nil {
			fmt.Println(resErr.TentError)
		}
		panic(err)
	}
}
<|endoftext|>


<commit_before>package sliceops

// Set of examples for all the functions

func ExampleAdd() {
	s1 := []float64{1, 2, 3, 4}
	s2 := []float64{5, 6, 7, 8}
	Add(s1, s2)
}
<commit_msg>Trying to get example to match godoc<commit_after>package sliceops

import (
	"fmt"
)

// Set of examples for all the functions

func ExampleAdd() {
	s1 := []float64{1, 2, 3, 4}
	s2 := []float64{5, 6, 7, 8}
	Add(s1, s2)
	fmt.Println("s1 = ", s1)
	fmt.Println("s2 = ", s2)
	// Output:
	// s1 = [6,8,10,12]
	// s2 = [6,8,10,12]
}
<|endoftext|>


<commit_before>package connectivity

import (
	"bufio"
	"log"
	"net/http"
	"strconv"
	"testing"
	"time"
)

// download test data from internet
const (
	tiny   string = "http://algs4.cs.princeton.edu/15uf/tinyUF.txt"
	medium string = "http://algs4.cs.princeton.edu/15uf/mediumUF.txt"
	huge   string = "http://algs4.cs.princeton.edu/15uf/largeUF.txt"
)

func loadTestDataFromWeb() []TestData {

	c := make(chan TestData)
	go func() { c <- parse(tiny) }()
	go func() { c <- parse(medium) }()
	go func() { c <- parse(huge) }()

	var results []TestData
	timeout := time.After(1 * time.Second)
	for i := 0; i < 3; i++ {
		select {
		case result := <-c:
			results = append(results, result)
		case <-timeout:
			log.Println("timed out fetching data")
			return results
		}

	}
	return results
}

func parse(url string) (result TestData) {

	cl := &http.Client{}
	r, err := cl.Get(url)
	if err != nil {
		log.Fatalln(err)
	}

	defer r.Body.Close()

	s := bufio.NewScanner(r.Body)
	s.Split(bufio.ScanWords)
	var pairs []Pair
	s.Scan()
	g, err := strconv.Atoi(s.Text())
	if err != nil {
		log.Panicf("first generator parse error %v", g, err)
	}
	for s.Scan() {
		var pair Pair
		pair.Left, err = strconv.Atoi(s.Text())
		if err != nil {
			log.Fatalf("parse left %v failed", pair.Left, err)
		}
		s.Scan()
		pair.Right, err = strconv.Atoi(s.Text())
		if err != nil {
			log.Fatalf("parse right %v failed", pair.Right, err)
		}
		pairs = append(pairs, pair)
	}

	result.Generator = g
	result.Pairs = pairs
	return result
}

type Pair struct {
	Left  int
	Right int
}

type TestData struct {
	Generator int
	Pairs     []Pair
}

func TestJustTest(t *testing.T) {
	loadStart := time.Now()
	n := loadTestDataFromWeb()
	log.Printf("loading used %v", time.Since(loadStart))
	for _, content := range n {
		log.Printf("Generator %v has %v pairs", content.Generator, len(content.Pairs))
		algoStart := time.Now()
		test := NewWeightedCompression(content.Generator)
		for idx, p := range content.Pairs {
			// log.Printf("%vth pair: %v and %v", idx, p.Left, p.Right)
			test.Union(p.Left, p.Right)
			if !test.Find(p.Left, p.Right) {
				t.Errorf("%v and %v are expected to be in the same component, this is the %vth union operation", p.Left, p.Right, idx)
			}
		}
		log.Printf("Weighted Compressed UnionFind used %v", time.Since(algoStart))
	}
}
<commit_msg>fix args mismatch<commit_after>package connectivity

import (
	"bufio"
	"log"
	"net/http"
	"strconv"
	"testing"
	"time"
)

// download test data from internet
const (
	tiny   string = "http://algs4.cs.princeton.edu/15uf/tinyUF.txt"
	medium string = "http://algs4.cs.princeton.edu/15uf/mediumUF.txt"
	huge   string = "http://algs4.cs.princeton.edu/15uf/largeUF.txt"
)

func loadTestDataFromWeb() []TestData {

	c := make(chan TestData)
	go func() { c <- parse(tiny) }()
	go func() { c <- parse(medium) }()
	go func() { c <- parse(huge) }()

	var results []TestData
	timeout := time.After(1 * time.Second)
	for i := 0; i < 3; i++ {
		select {
		case result := <-c:
			results = append(results, result)
		case <-timeout:
			log.Println("timed out fetching data")
			return results
		}

	}
	return results
}

func parse(url string) (result TestData) {

	cl := &http.Client{}
	r, err := cl.Get(url)
	if err != nil {
		log.Fatalln(err)
	}

	defer r.Body.Close()

	s := bufio.NewScanner(r.Body)
	s.Split(bufio.ScanWords)
	var pairs []Pair
	s.Scan()
	g, err := strconv.Atoi(s.Text())
	if err != nil {
		log.Panicf("first generator parse %v failed, error %v", s.Text(), err)
	}
	for s.Scan() {
		var pair Pair
		pair.Left, err = strconv.Atoi(s.Text())
		if err != nil {
			log.Fatalf("parse left %v failed, error %v", s.Text(), err)
		}
		s.Scan()
		pair.Right, err = strconv.Atoi(s.Text())
		if err != nil {
			log.Fatalf("parse right %v failed, error %v", s.Text(), err)
		}
		pairs = append(pairs, pair)
	}

	result.Generator = g
	result.Pairs = pairs
	return result
}

type Pair struct {
	Left  int
	Right int
}

type TestData struct {
	Generator int
	Pairs     []Pair
}

func TestJustTest(t *testing.T) {
	loadStart := time.Now()
	n := loadTestDataFromWeb()
	log.Printf("loading used %v", time.Since(loadStart))
	for _, content := range n {
		log.Printf("Generator %v has %v pairs", content.Generator, len(content.Pairs))
		algoStart := time.Now()
		test := NewWeightedCompression(content.Generator)
		for idx, p := range content.Pairs {
			// log.Printf("%vth pair: %v and %v", idx, p.Left, p.Right)
			test.Union(p.Left, p.Right)
			if !test.Find(p.Left, p.Right) {
				t.Errorf("%v and %v are expected to be in the same component, this is the %vth union operation", p.Left, p.Right, idx)
			}
		}
		log.Printf("Weighted Compressed UnionFind used %v", time.Since(algoStart))
	}
}
<|endoftext|>


<commit_before>package twitter

import (
	"fmt"

	log "github.com/Sirupsen/logrus"
	"github.com/dghubble/go-twitter/twitter"
	"github.com/dghubble/oauth1"
	"github.com/jirwin/quadlek/quadlek"
)

func load(consumerKey, consumerSecret, accessToken, accessSecret string, filter map[string]string) func(bot *quadlek.Bot, store *quadlek.Store) error {

	return func(bot *quadlek.Bot, store *quadlek.Store) error {
		go func() {
			config := oauth1.NewConfig(consumerKey, consumerSecret)
			token := oauth1.NewToken(accessToken, accessSecret)
			httpClient := config.Client(oauth1.NoContext, token)
			client := twitter.NewClient(httpClient)

			followFilters := []string{}
			for follow, _ := range filter {
				followFilters = append(followFilters, follow)
			}

			filterParams := &twitter.StreamFilterParams{
				Follow:        followFilters,
				StallWarnings: twitter.Bool(true),
			}

			stream, err := client.Streams.Filter(filterParams)
			if err != nil {
				log.WithField("err", err).Error("Error streaming tweets.")
				return
			}

			for {
				select {
				case msg := <-stream.Messages:
					switch m := msg.(type) {
					case *twitter.Tweet:
						if channel, ok := filter[m.User.IDStr]; ok {
							if m.RetweetedStatus != nil {
								log.WithField("tweet", m).Info("Got a tweet containing a retweet")
								if replyChannel, ok := filter[m.RetweetedStatus.User.IDStr]; ok && channel == replyChannel {
									log.WithField("tweet", m).Info("Tweet contains retweet from already monitored account, cancelling message")
									continue
								}
							}
							twitterUrl := fmt.Sprintf("https://twitter.com/%s/status/%s", m.User.ScreenName, m.IDStr)
							chanId, err := bot.GetChannelId(channel)
							if err != nil {
								log.WithField("err", err).Error("unable to find channel.")
								continue
							}
							bot.Say(chanId, twitterUrl)
						}
					}
				}
			}
		}()

		return nil
	}
}

func Register(consumerKey, consumerSecret, accessToken, accessSecret string, filter map[string]string) quadlek.Plugin {
	return quadlek.MakePlugin(
		"quadlek-twitter",
		nil,
		nil,
		nil,
		nil,
		load(consumerKey, consumerSecret, accessToken, accessSecret, filter),
	)
}
<commit_msg>Remove retweet detection, something seems wrong.<commit_after>package twitter

import (
	"fmt"

	log "github.com/Sirupsen/logrus"
	"github.com/dghubble/go-twitter/twitter"
	"github.com/dghubble/oauth1"
	"github.com/jirwin/quadlek/quadlek"
)

func load(consumerKey, consumerSecret, accessToken, accessSecret string, filter map[string]string) func(bot *quadlek.Bot, store *quadlek.Store) error {

	return func(bot *quadlek.Bot, store *quadlek.Store) error {
		go func() {
			config := oauth1.NewConfig(consumerKey, consumerSecret)
			token := oauth1.NewToken(accessToken, accessSecret)
			httpClient := config.Client(oauth1.NoContext, token)
			client := twitter.NewClient(httpClient)

			followFilters := []string{}
			for follow, _ := range filter {
				followFilters = append(followFilters, follow)
			}

			filterParams := &twitter.StreamFilterParams{
				Follow:        followFilters,
				StallWarnings: twitter.Bool(true),
			}

			stream, err := client.Streams.Filter(filterParams)
			if err != nil {
				log.WithField("err", err).Error("Error streaming tweets.")
				return
			}

			for {
				select {
				case msg := <-stream.Messages:
					switch m := msg.(type) {
					case *twitter.Tweet:
						if channel, ok := filter[m.User.IDStr]; ok {
							//if m.RetweetedStatus != nil {
							//	log.WithField("tweet", m).Info("Got a tweet containing a retweet")
							//	if replyChannel, ok := filter[m.RetweetedStatus.User.IDStr]; ok && channel == replyChannel {
							//		log.WithField("tweet", m).Info("Tweet contains retweet from already monitored account, cancelling message")
							//		continue
							//	}
							//}
							twitterUrl := fmt.Sprintf("https://twitter.com/%s/status/%s", m.User.ScreenName, m.IDStr)
							chanId, err := bot.GetChannelId(channel)
							if err != nil {
								log.WithField("err", err).Error("unable to find channel.")
								continue
							}
							bot.Say(chanId, twitterUrl)
						}
					}
				}
			}
		}()

		return nil
	}
}

func Register(consumerKey, consumerSecret, accessToken, accessSecret string, filter map[string]string) quadlek.Plugin {
	return quadlek.MakePlugin(
		"quadlek-twitter",
		nil,
		nil,
		nil,
		nil,
		load(consumerKey, consumerSecret, accessToken, accessSecret, filter),
	)
}
<|endoftext|>


<commit_before>package clicommand

import (
	"fmt"

	"github.com/buildkite/agent/agent"
	"github.com/buildkite/agent/api"
	"github.com/buildkite/agent/cliconfig"
	"github.com/urfave/cli"
)

var ShasumHelpDescription = `Usage:

   buildkite-agent artifact shasum [arguments...]

Description:

   Prints to STDOUT the SHA-1 for the artifact provided. If your search query
   for artifacts matches multiple agents, and error will be raised.

   Note: You need to ensure that your search query is surrounded by quotes if
   using a wild card as the built-in shell path globbing will provide files,
   which will break the download.

Example:

   $ buildkite-agent artifact shasum "pkg/release.tar.gz" --build xxx

   This will search for all the files in the build with the path "pkg/release.tar.gz" and will
   print to STDOUT it's SHA-1 checksum.

   If you would like to target artifacts from a specific build step, you can do
   so by using the --step argument.

   $ buildkite-agent artifact shasum "pkg/release.tar.gz" --step "release" --build xxx

   You can also use the step's job id (provided by the environment variable $BUILDKITE_JOB_ID)`

type ArtifactShasumConfig struct {
	Query string `cli:"arg:0" label:"artifact search query" validate:"required"`
	Step  string `cli:"step"`
	Build string `cli:"build" validate:"required"`

	// Global flags
	Debug   bool   `cli:"debug"`
	NoColor bool   `cli:"no-color"`
	Profile string `cli:"profile"`

	// API config
	DebugHTTP        bool   `cli:"debug-http"`
	AgentAccessToken string `cli:"agent-access-token" validate:"required"`
	Endpoint         string `cli:"endpoint" validate:"required"`
	NoHTTP2          bool   `cli:"no-http2"`
}

var ArtifactShasumCommand = cli.Command{
	Name:        "shasum",
	Usage:       "Prints the SHA-1 checksum for the artifact provided to STDOUT",
	Description: ShasumHelpDescription,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "step",
			Value: "",
			Usage: "Scope the search to a paticular step by using either it's name of job ID",
		},
		cli.StringFlag{
			Name:   "build",
			Value:  "",
			EnvVar: "BUILDKITE_BUILD_ID",
			Usage:  "The build that the artifacts were uploaded to",
		},

		// API Flags
		AgentAccessTokenFlag,
		EndpointFlag,
		NoHTTP2Flag,
		DebugHTTPFlag,

		// Global flags
		NoColorFlag,
		DebugFlag,
		ProfileFlag,
	},
	Action: func(c *cli.Context) {
		// The configuration will be loaded into this struct
		cfg := ArtifactShasumConfig{}

		l := CreateLogger(&cfg)

		// Load the configuration
		if err := cliconfig.Load(c, l, &cfg); err != nil {
			l.Fatal("%s", err)
		}

		// Setup any global configuration options
		done := HandleGlobalFlags(l, cfg)
		defer done()

		// Create the API client
		client := api.NewClient(l, loadAPIClientConfig(cfg, `AgentAccessToken`))

		// Find the artifact we want to show the SHASUM for
		searcher := agent.NewArtifactSearcher(l, client, cfg.Build)

		artifacts, err := searcher.Search(cfg.Query, cfg.Step)
		if err != nil {
			l.Fatal("Failed to find artifacts: %s", err)
		}

		artifactsFoundLength := len(artifacts)

		if artifactsFoundLength == 0 {
			l.Fatal("No artifacts found for downloading")
		} else if artifactsFoundLength > 1 {
			l.Fatal("Multiple artifacts were found. Try being more specific with the search or scope by step")
		} else {
			l.Debug("Artifact \"%s\" found", artifacts[0].Path)

			fmt.Printf("%s\n", artifacts[0].Sha1Sum)
		}
	},
}
<commit_msg>Fix typo<commit_after>package clicommand

import (
	"fmt"

	"github.com/buildkite/agent/agent"
	"github.com/buildkite/agent/api"
	"github.com/buildkite/agent/cliconfig"
	"github.com/urfave/cli"
)

var ShasumHelpDescription = `Usage:

   buildkite-agent artifact shasum [arguments...]

Description:

   Prints to STDOUT the SHA-1 for the artifact provided. If your search query
   for artifacts matches multiple agents, and error will be raised.

   Note: You need to ensure that your search query is surrounded by quotes if
   using a wild card as the built-in shell path globbing will provide files,
   which will break the download.

Example:

   $ buildkite-agent artifact shasum "pkg/release.tar.gz" --build xxx

   This will search for all the files in the build with the path "pkg/release.tar.gz" and will
   print to STDOUT it's SHA-1 checksum.

   If you would like to target artifacts from a specific build step, you can do
   so by using the --step argument.

   $ buildkite-agent artifact shasum "pkg/release.tar.gz" --step "release" --build xxx

   You can also use the step's job id (provided by the environment variable $BUILDKITE_JOB_ID)`

type ArtifactShasumConfig struct {
	Query string `cli:"arg:0" label:"artifact search query" validate:"required"`
	Step  string `cli:"step"`
	Build string `cli:"build" validate:"required"`

	// Global flags
	Debug   bool   `cli:"debug"`
	NoColor bool   `cli:"no-color"`
	Profile string `cli:"profile"`

	// API config
	DebugHTTP        bool   `cli:"debug-http"`
	AgentAccessToken string `cli:"agent-access-token" validate:"required"`
	Endpoint         string `cli:"endpoint" validate:"required"`
	NoHTTP2          bool   `cli:"no-http2"`
}

var ArtifactShasumCommand = cli.Command{
	Name:        "shasum",
	Usage:       "Prints the SHA-1 checksum for the artifact provided to STDOUT",
	Description: ShasumHelpDescription,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "step",
			Value: "",
			Usage: "Scope the search to a particular step by using either it's name of job ID",
		},
		cli.StringFlag{
			Name:   "build",
			Value:  "",
			EnvVar: "BUILDKITE_BUILD_ID",
			Usage:  "The build that the artifacts were uploaded to",
		},

		// API Flags
		AgentAccessTokenFlag,
		EndpointFlag,
		NoHTTP2Flag,
		DebugHTTPFlag,

		// Global flags
		NoColorFlag,
		DebugFlag,
		ProfileFlag,
	},
	Action: func(c *cli.Context) {
		// The configuration will be loaded into this struct
		cfg := ArtifactShasumConfig{}

		l := CreateLogger(&cfg)

		// Load the configuration
		if err := cliconfig.Load(c, l, &cfg); err != nil {
			l.Fatal("%s", err)
		}

		// Setup any global configuration options
		done := HandleGlobalFlags(l, cfg)
		defer done()

		// Create the API client
		client := api.NewClient(l, loadAPIClientConfig(cfg, `AgentAccessToken`))

		// Find the artifact we want to show the SHASUM for
		searcher := agent.NewArtifactSearcher(l, client, cfg.Build)

		artifacts, err := searcher.Search(cfg.Query, cfg.Step)
		if err != nil {
			l.Fatal("Failed to find artifacts: %s", err)
		}

		artifactsFoundLength := len(artifacts)

		if artifactsFoundLength == 0 {
			l.Fatal("No artifacts found for downloading")
		} else if artifactsFoundLength > 1 {
			l.Fatal("Multiple artifacts were found. Try being more specific with the search or scope by step")
		} else {
			l.Debug("Artifact \"%s\" found", artifacts[0].Path)

			fmt.Printf("%s\n", artifacts[0].Sha1Sum)
		}
	},
}
<|endoftext|>


<commit_before>package schema

import (
	"fmt"
	"reflect"
	"sort"
	"testing"

	"github.com/frictionlessdata/tableschema-go/table"
)

func Exampleinfer() {
	tab := table.FromSlices(
		[]string{"Person", "Height"},
		[][]string{
			[]string{"Foo", "5"},
			[]string{"Bar", "4"},
			[]string{"Bez", "5.5"},
		})
	s, _ := Infer(tab)
	fmt.Println("Fields:")
	for _, f := range s.Fields {
		fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format)
	}
	// Output: Fields:
	// {Name:Person Type:string Format:default}
	// {Name:Height Type:integer Format:default}
}

func ExampleInferImplicitCasting() {
	tab := table.FromSlices(
		[]string{"Person", "Height"},
		[][]string{
			[]string{"Foo", "5"},
			[]string{"Bar", "4"},
			[]string{"Bez", "5.5"},
		})
	s, _ := InferImplicitCasting(tab)
	fmt.Println("Fields:")
	for _, f := range s.Fields {
		fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format)
	}
	// Output: Fields:
	// {Name:Person Type:string Format:default}
	// {Name:Height Type:number Format:default}
}

func TestInfer_Success(t *testing.T) {
	data := []struct {
		desc    string
		headers []string
		table   [][]string
		want    Schema
	}{
		{"1Cell_Date", []string{"Birthday"}, [][]string{[]string{"1983-10-15"}}, Schema{Fields: []Field{{Name: "Birthday", Type: DateType, Format: defaultFieldFormat}}}},
		{"1Cell_Integer", []string{"Age"}, [][]string{[]string{"10"}}, Schema{Fields: []Field{{Name: "Age", Type: IntegerType, Format: defaultFieldFormat}}}},
		{"1Cell_Number", []string{"Weight"}, [][]string{[]string{"20.2"}}, Schema{Fields: []Field{{Name: "Weight", Type: NumberType, Format: defaultFieldFormat}}}},
		{"1Cell_Boolean", []string{"Foo"}, [][]string{[]string{"0"}}, Schema{Fields: []Field{{Name: "Foo", Type: BooleanType, Format: defaultFieldFormat}}}},
		{"1Cell_Object", []string{"Foo"}, [][]string{[]string{`{"name":"foo"}`}}, Schema{Fields: []Field{{Name: "Foo", Type: ObjectType, Format: defaultFieldFormat}}}},
		{"1Cell_Array", []string{"Foo"}, [][]string{[]string{`["name"]`}}, Schema{Fields: []Field{{Name: "Foo", Type: ArrayType, Format: defaultFieldFormat}}}},
		{"1Cell_String", []string{"Foo"}, [][]string{[]string{"name"}}, Schema{Fields: []Field{{Name: "Foo", Type: StringType, Format: defaultFieldFormat}}}},
		{"1Cell_Time", []string{"Foo"}, [][]string{[]string{"10:15:50"}}, Schema{Fields: []Field{{Name: "Foo", Type: TimeType, Format: defaultFieldFormat}}}},
		{"1Cell_YearMonth", []string{"YearMonth"}, [][]string{[]string{"2017-08"}}, Schema{Fields: []Field{{Name: "YearMonth", Type: YearMonthType, Format: defaultFieldFormat}}}},
		{"1Cell_Year", []string{"Year"}, [][]string{[]string{"2017"}}, Schema{Fields: []Field{{Name: "Year", Type: YearType, Format: defaultFieldFormat}}}},
		{"1Cell_DateTime", []string{"DateTime"}, [][]string{[]string{"2008-09-15T15:53:00+05:00"}}, Schema{Fields: []Field{{Name: "DateTime", Type: DateTimeType, Format: defaultFieldFormat}}}},
		{"1Cell_Duration", []string{"Duration"}, [][]string{[]string{"P3Y6M4DT12H30M5S"}}, Schema{Fields: []Field{{Name: "Duration", Type: DurationType, Format: defaultFieldFormat}}}},
		{"1Cell_GeoPoint", []string{"GeoPoint"}, [][]string{[]string{"90,45"}}, Schema{Fields: []Field{{Name: "GeoPoint", Type: GeoPointType, Format: defaultFieldFormat}}}},
		{"ManyCells",
			[]string{"Name", "Age", "Weight", "Bogus", "Boolean", "Boolean1"},
			[][]string{
				[]string{"Foo", "10", "20.2", "1", "1", "1"},
				[]string{"Foo", "10", "30", "1", "1", "1"},
				[]string{"Foo", "10", "30", "Daniel", "1", "2"},
			},
			Schema{Fields: []Field{
				{Name: "Name", Type: StringType, Format: defaultFieldFormat},
				{Name: "Age", Type: IntegerType, Format: defaultFieldFormat},
				{Name: "Weight", Type: IntegerType, Format: defaultFieldFormat},
				{Name: "Bogus", Type: BooleanType, Format: defaultFieldFormat},
				{Name: "Boolean", Type: BooleanType, Format: defaultFieldFormat},
				{Name: "Boolean1", Type: BooleanType, Format: defaultFieldFormat},
			}},
		},
	}
	for _, d := range data {
		t.Run(d.desc, func(t *testing.T) {
			s, err := infer(d.headers, d.table)
			if err != nil {
				t.Fatalf("want:nil, got:%q", err)
			}
			sort.Sort(s.Fields)
			sort.Sort(d.want.Fields)
			if !reflect.DeepEqual(s, &d.want) {
				t.Errorf("want:%+v, got:%+v", &d.want, s)
			}
		})
	}
}

func TestInfer_Error(t *testing.T) {
	data := []struct {
		desc    string
		headers []string
		table   [][]string
	}{
		{"NotATable", []string{}, [][]string{[]string{"1"}}},
	}
	for _, d := range data {
		t.Run(d.desc, func(t *testing.T) {
			_, err := infer(d.headers, d.table)
			if err == nil {
				t.Fatalf("want:error, got:nil")
			}
		})
	}
}

func TestInferImplicitCasting_Success(t *testing.T) {
	data := []struct {
		desc    string
		headers []string
		table   [][]string
		want    Schema
	}{
		{"1Cell_Date", []string{"Birthday"}, [][]string{[]string{"1983-10-15"}}, Schema{Fields: []Field{{Name: "Birthday", Type: DateType, Format: defaultFieldFormat}}}},
		{"1Cell_Integer", []string{"Age"}, [][]string{[]string{"10"}}, Schema{Fields: []Field{{Name: "Age", Type: IntegerType, Format: defaultFieldFormat}}}},
		{"1Cell_Number", []string{"Weight"}, [][]string{[]string{"20.2"}}, Schema{Fields: []Field{{Name: "Weight", Type: NumberType, Format: defaultFieldFormat}}}},
		{"1Cell_Boolean", []string{"Foo"}, [][]string{[]string{"0"}}, Schema{Fields: []Field{{Name: "Foo", Type: BooleanType, Format: defaultFieldFormat}}}},
		{"1Cell_Object", []string{"Foo"}, [][]string{[]string{`{"name":"foo"}`}}, Schema{Fields: []Field{{Name: "Foo", Type: ObjectType, Format: defaultFieldFormat}}}},
		{"1Cell_Array", []string{"Foo"}, [][]string{[]string{`["name"]`}}, Schema{Fields: []Field{{Name: "Foo", Type: ArrayType, Format: defaultFieldFormat}}}},
		{"1Cell_String", []string{"Foo"}, [][]string{[]string{"name"}}, Schema{Fields: []Field{{Name: "Foo", Type: StringType, Format: defaultFieldFormat}}}},
		{"1Cell_Time", []string{"Foo"}, [][]string{[]string{"10:15:50"}}, Schema{Fields: []Field{{Name: "Foo", Type: TimeType, Format: defaultFieldFormat}}}},
		{"1Cell_YearMonth", []string{"YearMonth"}, [][]string{[]string{"2017-08"}}, Schema{Fields: []Field{{Name: "YearMonth", Type: YearMonthType, Format: defaultFieldFormat}}}},
		{"1Cell_Year", []string{"Year"}, [][]string{[]string{"2017"}}, Schema{Fields: []Field{{Name: "Year", Type: YearType, Format: defaultFieldFormat}}}},
		{"1Cell_DateTime", []string{"DateTime"}, [][]string{[]string{"2008-09-15T15:53:00+05:00"}}, Schema{Fields: []Field{{Name: "DateTime", Type: DateTimeType, Format: defaultFieldFormat}}}},
		{"1Cell_Duration", []string{"Duration"}, [][]string{[]string{"P3Y6M4DT12H30M5S"}}, Schema{Fields: []Field{{Name: "Duration", Type: DurationType, Format: defaultFieldFormat}}}},
		{"1Cell_GeoPoint", []string{"GeoPoint"}, [][]string{[]string{"90,45"}}, Schema{Fields: []Field{{Name: "GeoPoint", Type: GeoPointType, Format: defaultFieldFormat}}}},
		{"ManyCells",
			[]string{"Name", "Age", "Weight", "Bogus", "Boolean", "Int"},
			[][]string{
				[]string{"Foo", "10", "20.2", "1", "1", "1"},
				[]string{"Foo", "10", "30", "1", "1", "1"},
				[]string{"Foo", "10", "30", "Daniel", "1", "2"},
			},
			Schema{Fields: []Field{
				{Name: "Name", Type: StringType, Format: defaultFieldFormat},
				{Name: "Age", Type: IntegerType, Format: defaultFieldFormat},
				{Name: "Weight", Type: NumberType, Format: defaultFieldFormat},
				{Name: "Bogus", Type: StringType, Format: defaultFieldFormat},
				{Name: "Boolean", Type: BooleanType, Format: defaultFieldFormat},
				{Name: "Int", Type: IntegerType, Format: defaultFieldFormat},
			}},
		},
	}
	for _, d := range data {
		t.Run(d.desc, func(t *testing.T) {
			s, err := inferImplicitCasting(d.headers, d.table)
			if err != nil {
				t.Fatalf("want:nil, got:%q", err)
			}
			sort.Sort(s.Fields)
			sort.Sort(d.want.Fields)
			if !reflect.DeepEqual(s, &d.want) {
				t.Errorf("want:%+v, got:%+v", d.want, s)
			}
		})
	}
}

func TestInferImplicitCasting_Error(t *testing.T) {
	data := []struct {
		desc    string
		headers []string
		table   [][]string
	}{
		{"NotATable", []string{}, [][]string{[]string{"1"}}},
	}
	for _, d := range data {
		t.Run(d.desc, func(t *testing.T) {
			_, err := inferImplicitCasting(d.headers, d.table)
			if err == nil {
				t.Fatalf("want:error, got:nil")
			}
		})
	}
}

var (
	benchmarkHeaders = []string{"Name", "Birthday", "Weight", "Address", "Siblings"}
	benchmarkTable   = [][]string{
		[]string{"Foo", "2015-10-12", "20.2", `{"Street":"Foo", "Number":10, "City":"New York", "State":"NY"}`, `["Foo"]`},
		[]string{"Bar", "2015-10-12", "30", `{"Street":"Foo", "Number":10, "City":"New York", "State":"NY"}`, `["Foo"]`},
		[]string{"Bez", "2015-10-12", "30", `{"Street":"Foo", "Number":10, "City":"New York", "State":"NY"}`, `["Foo"]`},
	}
)

func benchmarkinfer(growthMultiplier int, b *testing.B) {
	for n := 0; n < b.N; n++ {
		infer(benchmarkHeaders, generateBenchmarkTable(growthMultiplier))
	}
}

func benchmarkInferImplicitCasting(growthMultiplier int, b *testing.B) {
	for n := 0; n < b.N; n++ {
		inferImplicitCasting(benchmarkHeaders, generateBenchmarkTable(growthMultiplier))
	}
}

func generateBenchmarkTable(growthMultiplier int) [][]string {
	var t [][]string
	for i := 0; i < growthMultiplier; i++ {
		t = append(t, benchmarkTable...)
	}
	return t
}

func BenchmarkInferSmall(b *testing.B)                 { benchmarkinfer(1, b) }
func BenchmarkInferMedium(b *testing.B)                { benchmarkinfer(100, b) }
func BenchmarkInferBig(b *testing.B)                   { benchmarkinfer(1000, b) }
func BenchmarkInferImplicitCastingSmall(b *testing.B)  { benchmarkInferImplicitCasting(1, b) }
func BenchmarkInferImplicitCastingMedium(b *testing.B) { benchmarkInferImplicitCasting(100, b) }
func BenchmarkInferImplicitCastingBig(b *testing.B)    { benchmarkInferImplicitCasting(1000, b) }
<commit_msg>Making test names conform to methods in infer package<commit_after>package schema

import (
	"fmt"
	"reflect"
	"sort"
	"testing"

	"github.com/frictionlessdata/tableschema-go/table"
)

func Exampleinfer() {
	tab := table.FromSlices(
		[]string{"Person", "Height"},
		[][]string{
			[]string{"Foo", "5"},
			[]string{"Bar", "4"},
			[]string{"Bez", "5.5"},
		})
	s, _ := Infer(tab)
	fmt.Println("Fields:")
	for _, f := range s.Fields {
		fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format)
	}
	// Output: Fields:
	// {Name:Person Type:string Format:default}
	// {Name:Height Type:integer Format:default}
}

func ExampleInferImplicitCasting() {
	tab := table.FromSlices(
		[]string{"Person", "Height"},
		[][]string{
			[]string{"Foo", "5"},
			[]string{"Bar", "4"},
			[]string{"Bez", "5.5"},
		})
	s, _ := InferImplicitCasting(tab)
	fmt.Println("Fields:")
	for _, f := range s.Fields {
		fmt.Printf("{Name:%s Type:%s Format:%s}\n", f.Name, f.Type, f.Format)
	}
	// Output: Fields:
	// {Name:Person Type:string Format:default}
	// {Name:Height Type:number Format:default}
}

func TestInfer(t *testing.T) {
	data := []struct {
		desc    string
		headers []string
		table   [][]string
		want    Schema
	}{
		{"1Cell_Date", []string{"Birthday"}, [][]string{[]string{"1983-10-15"}}, Schema{Fields: []Field{{Name: "Birthday", Type: DateType, Format: defaultFieldFormat}}}},
		{"1Cell_Integer", []string{"Age"}, [][]string{[]string{"10"}}, Schema{Fields: []Field{{Name: "Age", Type: IntegerType, Format: defaultFieldFormat}}}},
		{"1Cell_Number", []string{"Weight"}, [][]string{[]string{"20.2"}}, Schema{Fields: []Field{{Name: "Weight", Type: NumberType, Format: defaultFieldFormat}}}},
		{"1Cell_Boolean", []string{"Foo"}, [][]string{[]string{"0"}}, Schema{Fields: []Field{{Name: "Foo", Type: BooleanType, Format: defaultFieldFormat}}}},
		{"1Cell_Object", []string{"Foo"}, [][]string{[]string{`{"name":"foo"}`}}, Schema{Fields: []Field{{Name: "Foo", Type: ObjectType, Format: defaultFieldFormat}}}},
		{"1Cell_Array", []string{"Foo"}, [][]string{[]string{`["name"]`}}, Schema{Fields: []Field{{Name: "Foo", Type: ArrayType, Format: defaultFieldFormat}}}},
		{"1Cell_String", []string{"Foo"}, [][]string{[]string{"name"}}, Schema{Fields: []Field{{Name: "Foo", Type: StringType, Format: defaultFieldFormat}}}},
		{"1Cell_Time", []string{"Foo"}, [][]string{[]string{"10:15:50"}}, Schema{Fields: []Field{{Name: "Foo", Type: TimeType, Format: defaultFieldFormat}}}},
		{"1Cell_YearMonth", []string{"YearMonth"}, [][]string{[]string{"2017-08"}}, Schema{Fields: []Field{{Name: "YearMonth", Type: YearMonthType, Format: defaultFieldFormat}}}},
		{"1Cell_Year", []string{"Year"}, [][]string{[]string{"2017"}}, Schema{Fields: []Field{{Name: "Year", Type: YearType, Format: defaultFieldFormat}}}},
		{"1Cell_DateTime", []string{"DateTime"}, [][]string{[]string{"2008-09-15T15:53:00+05:00"}}, Schema{Fields: []Field{{Name: "DateTime", Type: DateTimeType, Format: defaultFieldFormat}}}},
		{"1Cell_Duration", []string{"Duration"}, [][]string{[]string{"P3Y6M4DT12H30M5S"}}, Schema{Fields: []Field{{Name: "Duration", Type: DurationType, Format: defaultFieldFormat}}}},
		{"1Cell_GeoPoint", []string{"GeoPoint"}, [][]string{[]string{"90,45"}}, Schema{Fields: []Field{{Name: "GeoPoint", Type: GeoPointType, Format: defaultFieldFormat}}}},
		{"ManyCells",
			[]string{"Name", "Age", "Weight", "Bogus", "Boolean", "Boolean1"},
			[][]string{
				[]string{"Foo", "10", "20.2", "1", "1", "1"},
				[]string{"Foo", "10", "30", "1", "1", "1"},
				[]string{"Foo", "10", "30", "Daniel", "1", "2"},
			},
			Schema{Fields: []Field{
				{Name: "Name", Type: StringType, Format: defaultFieldFormat},
				{Name: "Age", Type: IntegerType, Format: defaultFieldFormat},
				{Name: "Weight", Type: IntegerType, Format: defaultFieldFormat},
				{Name: "Bogus", Type: BooleanType, Format: defaultFieldFormat},
				{Name: "Boolean", Type: BooleanType, Format: defaultFieldFormat},
				{Name: "Boolean1", Type: BooleanType, Format: defaultFieldFormat},
			}},
		},
	}
	for _, d := range data {
		t.Run(d.desc, func(t *testing.T) {
			s, err := infer(d.headers, d.table)
			if err != nil {
				t.Fatalf("want:nil, got:%q", err)
			}
			sort.Sort(s.Fields)
			sort.Sort(d.want.Fields)
			if !reflect.DeepEqual(s, &d.want) {
				t.Errorf("want:%+v, got:%+v", &d.want, s)
			}
		})
	}
	t.Run("Error", func(t *testing.T) {
		data := []struct {
			desc    string
			headers []string
			table   [][]string
		}{
			{"NotATable", []string{}, [][]string{[]string{"1"}}},
		}
		for _, d := range data {
			t.Run(d.desc, func(t *testing.T) {
				_, err := infer(d.headers, d.table)
				if err == nil {
					t.Fatalf("want:error, got:nil")
				}
			})
		}
	})
}

func TestInferImplicitCasting(t *testing.T) {
	data := []struct {
		desc    string
		headers []string
		table   [][]string
		want    Schema
	}{
		{"1Cell_Date", []string{"Birthday"}, [][]string{[]string{"1983-10-15"}}, Schema{Fields: []Field{{Name: "Birthday", Type: DateType, Format: defaultFieldFormat}}}},
		{"1Cell_Integer", []string{"Age"}, [][]string{[]string{"10"}}, Schema{Fields: []Field{{Name: "Age", Type: IntegerType, Format: defaultFieldFormat}}}},
		{"1Cell_Number", []string{"Weight"}, [][]string{[]string{"20.2"}}, Schema{Fields: []Field{{Name: "Weight", Type: NumberType, Format: defaultFieldFormat}}}},
		{"1Cell_Boolean", []string{"Foo"}, [][]string{[]string{"0"}}, Schema{Fields: []Field{{Name: "Foo", Type: BooleanType, Format: defaultFieldFormat}}}},
		{"1Cell_Object", []string{"Foo"}, [][]string{[]string{`{"name":"foo"}`}}, Schema{Fields: []Field{{Name: "Foo", Type: ObjectType, Format: defaultFieldFormat}}}},
		{"1Cell_Array", []string{"Foo"}, [][]string{[]string{`["name"]`}}, Schema{Fields: []Field{{Name: "Foo", Type: ArrayType, Format: defaultFieldFormat}}}},
		{"1Cell_String", []string{"Foo"}, [][]string{[]string{"name"}}, Schema{Fields: []Field{{Name: "Foo", Type: StringType, Format: defaultFieldFormat}}}},
		{"1Cell_Time", []string{"Foo"}, [][]string{[]string{"10:15:50"}}, Schema{Fields: []Field{{Name: "Foo", Type: TimeType, Format: defaultFieldFormat}}}},
		{"1Cell_YearMonth", []string{"YearMonth"}, [][]string{[]string{"2017-08"}}, Schema{Fields: []Field{{Name: "YearMonth", Type: YearMonthType, Format: defaultFieldFormat}}}},
		{"1Cell_Year", []string{"Year"}, [][]string{[]string{"2017"}}, Schema{Fields: []Field{{Name: "Year", Type: YearType, Format: defaultFieldFormat}}}},
		{"1Cell_DateTime", []string{"DateTime"}, [][]string{[]string{"2008-09-15T15:53:00+05:00"}}, Schema{Fields: []Field{{Name: "DateTime", Type: DateTimeType, Format: defaultFieldFormat}}}},
		{"1Cell_Duration", []string{"Duration"}, [][]string{[]string{"P3Y6M4DT12H30M5S"}}, Schema{Fields: []Field{{Name: "Duration", Type: DurationType, Format: defaultFieldFormat}}}},
		{"1Cell_GeoPoint", []string{"GeoPoint"}, [][]string{[]string{"90,45"}}, Schema{Fields: []Field{{Name: "GeoPoint", Type: GeoPointType, Format: defaultFieldFormat}}}},
		{"ManyCells",
			[]string{"Name", "Age", "Weight", "Bogus", "Boolean", "Int"},
			[][]string{
				[]string{"Foo", "10", "20.2", "1", "1", "1"},
				[]string{"Foo", "10", "30", "1", "1", "1"},
				[]string{"Foo", "10", "30", "Daniel", "1", "2"},
			},
			Schema{Fields: []Field{
				{Name: "Name", Type: StringType, Format: defaultFieldFormat},
				{Name: "Age", Type: IntegerType, Format: defaultFieldFormat},
				{Name: "Weight", Type: NumberType, Format: defaultFieldFormat},
				{Name: "Bogus", Type: StringType, Format: defaultFieldFormat},
				{Name: "Boolean", Type: BooleanType, Format: defaultFieldFormat},
				{Name: "Int", Type: IntegerType, Format: defaultFieldFormat},
			}},
		},
	}
	for _, d := range data {
		t.Run(d.desc, func(t *testing.T) {
			s, err := inferImplicitCasting(d.headers, d.table)
			if err != nil {
				t.Fatalf("want:nil, got:%q", err)
			}
			sort.Sort(s.Fields)
			sort.Sort(d.want.Fields)
			if !reflect.DeepEqual(s, &d.want) {
				t.Errorf("want:%+v, got:%+v", d.want, s)
			}
		})
	}
	t.Run("Error", func(t *testing.T) {
		data := []struct {
			desc    string
			headers []string
			table   [][]string
		}{
			{"NotATable", []string{}, [][]string{[]string{"1"}}},
		}
		for _, d := range data {
			t.Run(d.desc, func(t *testing.T) {
				_, err := inferImplicitCasting(d.headers, d.table)
				if err == nil {
					t.Fatalf("want:error, got:nil")
				}
			})
		}
	})
}

var (
	benchmarkHeaders = []string{"Name", "Birthday", "Weight", "Address", "Siblings"}
	benchmarkTable   = [][]string{
		[]string{"Foo", "2015-10-12", "20.2", `{"Street":"Foo", "Number":10, "City":"New York", "State":"NY"}`, `["Foo"]`},
		[]string{"Bar", "2015-10-12", "30", `{"Street":"Foo", "Number":10, "City":"New York", "State":"NY"}`, `["Foo"]`},
		[]string{"Bez", "2015-10-12", "30", `{"Street":"Foo", "Number":10, "City":"New York", "State":"NY"}`, `["Foo"]`},
	}
)

func benchmarkinfer(growthMultiplier int, b *testing.B) {
	for n := 0; n < b.N; n++ {
		infer(benchmarkHeaders, generateBenchmarkTable(growthMultiplier))
	}
}

func benchmarkInferImplicitCasting(growthMultiplier int, b *testing.B) {
	for n := 0; n < b.N; n++ {
		inferImplicitCasting(benchmarkHeaders, generateBenchmarkTable(growthMultiplier))
	}
}

func generateBenchmarkTable(growthMultiplier int) [][]string {
	var t [][]string
	for i := 0; i < growthMultiplier; i++ {
		t = append(t, benchmarkTable...)
	}
	return t
}

func BenchmarkInferSmall(b *testing.B)                 { benchmarkinfer(1, b) }
func BenchmarkInferMedium(b *testing.B)                { benchmarkinfer(100, b) }
func BenchmarkInferBig(b *testing.B)                   { benchmarkinfer(1000, b) }
func BenchmarkInferImplicitCastingSmall(b *testing.B)  { benchmarkInferImplicitCasting(1, b) }
func BenchmarkInferImplicitCastingMedium(b *testing.B) { benchmarkInferImplicitCasting(100, b) }
func BenchmarkInferImplicitCastingBig(b *testing.B)    { benchmarkInferImplicitCasting(1000, b) }
<|endoftext|>


<commit_before>package core

import (
	"context"
	"time"

	"chain/core/generator"
	"chain/database/pg"
	"chain/errors"
	"chain/protocol"
	"chain/protocol/vmutil"
)

// ErrProdReset is returned when reset is called on a
// production system.
var ErrProdReset = errors.New("reset called on production system")

func (a *api) reset(ctx context.Context) error {
	lastBlock, err := a.c.LatestBlock(ctx)
	if err != nil {
		return errors.Wrap(err)
	}

	keys, quorum, err := vmutil.ParseBlockMultiSigScript(lastBlock.ConsensusProgram)
	if err != nil {
		return errors.Wrap(err)
	}

	if len(keys) != 0 {
		return ErrProdReset
	}

	const q = `
		TRUNCATE
			account_control_programs,
			account_utxos,
			accounts,
			annotated_accounts,
			annotated_assets,
			annotated_outputs,
			annotated_txs,
			asset_tags,
			assets,
			blocks,
			blocks_txs,
			generator_pending_block,
			issuance_totals,
			leader,
			mockhsm,
			pool_txs,
			query_blocks,
			query_indexes,
			reservations,
			signed_blocks,
			signers,
			snapshots,
			txs
			RESTART IDENTITY;
	`

	_, err = pg.Exec(ctx, q)
	if err != nil {
		return errors.Wrap(err)
	}

	block, err := protocol.NewGenesisBlock(keys, quorum, time.Now())
	if err != nil {
		return errors.Wrap(err)
	}

	err = generator.SaveInitialBlock(ctx, pg.FromContext(ctx), block)
	if err != nil {
		return errors.Wrap(err)
	}

	return nil
}
<commit_msg>core: fix reset with no blocks<commit_after>package core

import (
	"context"
	"time"

	"chain/core/generator"
	"chain/crypto/ed25519"
	"chain/database/pg"
	"chain/errors"
	"chain/protocol"
	"chain/protocol/vmutil"
)

func getBlockKeys(c *protocol.Chain, ctx context.Context) (keys []ed25519.PublicKey, quorum int, err error) {
	lastBlock, err := c.LatestBlock(ctx)
	if err == protocol.ErrNoBlocks {
		return nil, 0, nil
	}
	if err != nil {
		return nil, 0, errors.Wrap(err)
	}
	return vmutil.ParseBlockMultiSigScript(lastBlock.ConsensusProgram)
}

// ErrProdReset is returned when reset is called on a
// production system.
var ErrProdReset = errors.New("reset called on production system")

func (a *api) reset(ctx context.Context) error {
	keys, quorum, err := getBlockKeys(a.c, ctx)
	if err != nil {
		return errors.Wrap(err)
	}

	if len(keys) != 0 {
		return ErrProdReset
	}

	const q = `
		TRUNCATE
			account_control_programs,
			account_utxos,
			accounts,
			annotated_accounts,
			annotated_assets,
			annotated_outputs,
			annotated_txs,
			asset_tags,
			assets,
			blocks,
			blocks_txs,
			generator_pending_block,
			issuance_totals,
			leader,
			mockhsm,
			pool_txs,
			query_blocks,
			query_indexes,
			reservations,
			signed_blocks,
			signers,
			snapshots,
			txs
			RESTART IDENTITY;
	`

	_, err = pg.Exec(ctx, q)
	if err != nil {
		return errors.Wrap(err)
	}

	block, err := protocol.NewGenesisBlock(keys, quorum, time.Now())
	if err != nil {
		return errors.Wrap(err)
	}

	err = generator.SaveInitialBlock(ctx, pg.FromContext(ctx), block)
	if err != nil {
		return errors.Wrap(err)
	}

	return nil
}
<|endoftext|>