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

package zlib

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"testing"
)

var filenames = []string{
	"../testdata/e.txt",
	"../testdata/pi.txt",
}

var data = []string{
	"test a reasonable sized string that can be compressed",
}

// Tests that compressing and then decompressing the given file at the given compression level and dictionary
// yields equivalent bytes to the original file.
func testFileLevelDict(t *testing.T, fn string, level int, d string) {
	// Read the file, as golden output.
	golden, err := os.Open(fn)
	if err != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
		return
	}
	defer golden.Close()
	b0, err0 := ioutil.ReadAll(golden)
	if err0 != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err0)
		return
	}
	testLevelDict(t, fn, b0, level, d)
}

func testLevelDict(t *testing.T, fn string, b0 []byte, level int, d string) {
	// Make dictionary, if given.
	var dict []byte
	if d != "" {
		dict = []byte(d)
	}

	// Push data through a pipe that compresses at the write end, and decompresses at the read end.
	piper, pipew := io.Pipe()
	defer piper.Close()
	go func() {
		defer pipew.Close()
		zlibw, err := NewWriterLevelDict(pipew, level, dict)
		if err != nil {
			t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
			return
		}
		defer zlibw.Close()
		_, err = zlibw.Write(b0)
		if err != nil {
			t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
			return
		}
	}()
	zlibr, err := NewReaderDict(piper, dict)
	if err != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
		return
	}
	defer zlibr.Close()

	// Compare the decompressed data.
	b1, err1 := ioutil.ReadAll(zlibr)
	if err1 != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err1)
		return
	}
	if len(b0) != len(b1) {
		t.Errorf("%s (level=%d, dict=%q): length mismatch %d versus %d", fn, level, d, len(b0), len(b1))
		return
	}
	for i := 0; i < len(b0); i++ {
		if b0[i] != b1[i] {
			t.Errorf("%s (level=%d, dict=%q): mismatch at %d, 0x%02x versus 0x%02x\n", fn, level, d, i, b0[i], b1[i])
			return
		}
	}
}

func testFileLevelDictReset(t *testing.T, fn string, level int, dict []byte) {
	var b0 []byte
	var err error
	if fn != "" {
		b0, err = ioutil.ReadFile(fn)
		if err != nil {
			t.Errorf("%s (level=%d): %v", fn, level, err)
			return
		}
	}

	// Compress once.
	buf := new(bytes.Buffer)
	var zlibw *Writer
	if dict == nil {
		zlibw, err = NewWriterLevel(buf, level)
	} else {
		zlibw, err = NewWriterLevelDict(buf, level, dict)
	}
	if err == nil {
		_, err = zlibw.Write(b0)
	}
	if err == nil {
		err = zlibw.Close()
	}
	if err != nil {
		t.Errorf("%s (level=%d): %v", fn, level, err)
		return
	}
	out := buf.String()

	// Reset and compress again.
	buf2 := new(bytes.Buffer)
	zlibw.Reset(buf2)
	_, err = zlibw.Write(b0)
	if err == nil {
		err = zlibw.Close()
	}
	if err != nil {
		t.Errorf("%s (level=%d): %v", fn, level, err)
		return
	}
	out2 := buf2.String()

	if out2 != out {
		t.Errorf("%s (level=%d): different output after reset (got %d bytes, expected %d",
			fn, level, len(out2), len(out))
	}
}

func TestWriter(t *testing.T) {
	for i, s := range data {
		b := []byte(s)
		tag := fmt.Sprintf("#%d", i)
		testLevelDict(t, tag, b, DefaultCompression, "")
		testLevelDict(t, tag, b, NoCompression, "")
		for level := BestSpeed; level <= BestCompression; level++ {
			testLevelDict(t, tag, b, level, "")
		}
	}
}

func TestWriterBig(t *testing.T) {
	for _, fn := range filenames {
		testFileLevelDict(t, fn, DefaultCompression, "")
		testFileLevelDict(t, fn, NoCompression, "")
		for level := BestSpeed; level <= BestCompression; level++ {
			testFileLevelDict(t, fn, level, "")
		}
	}
}

func TestWriterDict(t *testing.T) {
	const dictionary = "0123456789."
	for _, fn := range filenames {
		testFileLevelDict(t, fn, DefaultCompression, dictionary)
		testFileLevelDict(t, fn, NoCompression, dictionary)
		for level := BestSpeed; level <= BestCompression; level++ {
			testFileLevelDict(t, fn, level, dictionary)
		}
	}
}

func TestWriterReset(t *testing.T) {
	const dictionary = "0123456789."
	for _, fn := range filenames {
		testFileLevelDictReset(t, fn, NoCompression, nil)
		testFileLevelDictReset(t, fn, DefaultCompression, nil)
		testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary))
		testFileLevelDictReset(t, fn, DefaultCompression, []byte(dictionary))
		if !testing.Short() {
			for level := BestSpeed; level <= BestCompression; level++ {
				testFileLevelDictReset(t, fn, level, nil)
			}
		}
	}
}

func TestWriterDictIsUsed(t *testing.T) {
	var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
	var buf bytes.Buffer
	compressor, err := NewWriterLevelDict(&buf, BestCompression, input)
	if err != nil {
		t.Errorf("error in NewWriterLevelDict: %s", err)
		return
	}
	compressor.Write(input)
	compressor.Close()
	const expectedMaxSize = 35
	output := buf.Bytes()
	if len(output) > expectedMaxSize {
		t.Errorf("result too large (got %d, want <= %d bytes). Is the dictionary being used?", len(output), expectedMaxSize)
	}
}
<commit_msg>Test Reset of constant time compression.<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zlib

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"testing"
)

var filenames = []string{
	"../testdata/e.txt",
	"../testdata/pi.txt",
}

var data = []string{
	"test a reasonable sized string that can be compressed",
}

// Tests that compressing and then decompressing the given file at the given compression level and dictionary
// yields equivalent bytes to the original file.
func testFileLevelDict(t *testing.T, fn string, level int, d string) {
	// Read the file, as golden output.
	golden, err := os.Open(fn)
	if err != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
		return
	}
	defer golden.Close()
	b0, err0 := ioutil.ReadAll(golden)
	if err0 != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err0)
		return
	}
	testLevelDict(t, fn, b0, level, d)
}

func testLevelDict(t *testing.T, fn string, b0 []byte, level int, d string) {
	// Make dictionary, if given.
	var dict []byte
	if d != "" {
		dict = []byte(d)
	}

	// Push data through a pipe that compresses at the write end, and decompresses at the read end.
	piper, pipew := io.Pipe()
	defer piper.Close()
	go func() {
		defer pipew.Close()
		zlibw, err := NewWriterLevelDict(pipew, level, dict)
		if err != nil {
			t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
			return
		}
		defer zlibw.Close()
		_, err = zlibw.Write(b0)
		if err != nil {
			t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
			return
		}
	}()
	zlibr, err := NewReaderDict(piper, dict)
	if err != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err)
		return
	}
	defer zlibr.Close()

	// Compare the decompressed data.
	b1, err1 := ioutil.ReadAll(zlibr)
	if err1 != nil {
		t.Errorf("%s (level=%d, dict=%q): %v", fn, level, d, err1)
		return
	}
	if len(b0) != len(b1) {
		t.Errorf("%s (level=%d, dict=%q): length mismatch %d versus %d", fn, level, d, len(b0), len(b1))
		return
	}
	for i := 0; i < len(b0); i++ {
		if b0[i] != b1[i] {
			t.Errorf("%s (level=%d, dict=%q): mismatch at %d, 0x%02x versus 0x%02x\n", fn, level, d, i, b0[i], b1[i])
			return
		}
	}
}

func testFileLevelDictReset(t *testing.T, fn string, level int, dict []byte) {
	var b0 []byte
	var err error
	if fn != "" {
		b0, err = ioutil.ReadFile(fn)
		if err != nil {
			t.Errorf("%s (level=%d): %v", fn, level, err)
			return
		}
	}

	// Compress once.
	buf := new(bytes.Buffer)
	var zlibw *Writer
	if dict == nil {
		zlibw, err = NewWriterLevel(buf, level)
	} else {
		zlibw, err = NewWriterLevelDict(buf, level, dict)
	}
	if err == nil {
		_, err = zlibw.Write(b0)
	}
	if err == nil {
		err = zlibw.Close()
	}
	if err != nil {
		t.Errorf("%s (level=%d): %v", fn, level, err)
		return
	}
	out := buf.String()

	// Reset and compress again.
	buf2 := new(bytes.Buffer)
	zlibw.Reset(buf2)
	n, err := zlibw.Write(b0)
	if err == nil {
		if int(n) != len(b0) {
			t.Fatal("Short write:", n, "!=", len(b0))
		}
		err = zlibw.Close()
	}
	if err != nil {
		t.Errorf("%s (level=%d): %v", fn, level, err)
		return
	}
	out2 := buf2.String()

	if out2 != out {
		t.Errorf("%s (level=%d): different output after reset (got %d bytes, expected %d)",
			fn, level, len(out2), len(out))
	}
}

func TestWriter(t *testing.T) {
	for i, s := range data {
		b := []byte(s)
		tag := fmt.Sprintf("#%d", i)
		testLevelDict(t, tag, b, DefaultCompression, "")
		testLevelDict(t, tag, b, NoCompression, "")
		for level := BestSpeed; level <= BestCompression; level++ {
			testLevelDict(t, tag, b, level, "")
		}
	}
}

func TestWriterBig(t *testing.T) {
	for _, fn := range filenames {
		testFileLevelDict(t, fn, DefaultCompression, "")
		testFileLevelDict(t, fn, NoCompression, "")
		for level := BestSpeed; level <= BestCompression; level++ {
			testFileLevelDict(t, fn, level, "")
		}
	}
}

func TestWriterDict(t *testing.T) {
	const dictionary = "0123456789."
	for _, fn := range filenames {
		testFileLevelDict(t, fn, DefaultCompression, dictionary)
		testFileLevelDict(t, fn, NoCompression, dictionary)
		for level := BestSpeed; level <= BestCompression; level++ {
			testFileLevelDict(t, fn, level, dictionary)
		}
	}
}

func TestWriterReset(t *testing.T) {
	const dictionary = "0123456789."
	for _, fn := range filenames {
		testFileLevelDictReset(t, fn, NoCompression, nil)
		testFileLevelDictReset(t, fn, DefaultCompression, nil)
		testFileLevelDictReset(t, fn, ConstantCompression, nil)
		testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary))
		testFileLevelDictReset(t, fn, DefaultCompression, []byte(dictionary))
		testFileLevelDictReset(t, fn, ConstantCompression, []byte(dictionary))
		if !testing.Short() {
			for level := BestSpeed; level <= BestCompression; level++ {
				testFileLevelDictReset(t, fn, level, nil)
			}
		}
	}
}

func TestWriterDictIsUsed(t *testing.T) {
	var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
	var buf bytes.Buffer
	compressor, err := NewWriterLevelDict(&buf, BestCompression, input)
	if err != nil {
		t.Errorf("error in NewWriterLevelDict: %s", err)
		return
	}
	compressor.Write(input)
	compressor.Close()
	const expectedMaxSize = 35
	output := buf.Bytes()
	if len(output) > expectedMaxSize {
		t.Errorf("result too large (got %d, want <= %d bytes). Is the dictionary being used?", len(output), expectedMaxSize)
	}
}
<|endoftext|>


<commit_before>package spoukfw

import (
	"github.com/julienschmidt/httprouter"
	"net/http"
	"fmt"
	"strings"
//"runtime"
//"reflect"
	"log"

)

type (
	spoukrouter struct {
		router      httprouter.Router
		//middlewares *spoukmiddlewares
		middlewares *spoukstockmiddlewares
		spoukmux    *Spoukmux
	}
	SpoukHandler func(*SpoukCarry) error
	spoukMapRoute struct {
		Path    string
		Method  string
		Handler string
	}
)

var (
	validMethods map[string]string = map[string]string{
		"GET" : "GET",
		"POST" : "POST",
		"UPDATE": "UPDATE",
		"DELETE" : "DELETE",
	}
)

func newSpoukRouter(mux *Spoukmux) *spoukrouter {
	sp := &spoukrouter{
		spoukmux:mux,
		router:httprouter.Router{HandleMethodNotAllowed:true, RedirectFixedPath:true, RedirectTrailingSlash:true},
	}
	return sp
}
//---------------------------------------------------------------------------
//  SPOUKROUTER
//---------------------------------------------------------------------------
//     router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
func (sr *spoukrouter) StaticFiles(realpath, wwwpath string) {
	sr.router.ServeFiles(realpath, http.Dir(wwwpath))
}
func (sr spoukrouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sr.router.ServeHTTP(w, r)
}

func (sr *spoukrouter) fixPrefix(prefix string) string {
	//убирает слеш последний заебанный
	if len(strings.TrimSpace(prefix)) == 1 && prefix == "/" {
		return ""
	}
	if len(strings.TrimSpace(prefix)) > 1 {
		if prefix[len(prefix) - 1:] == "/" {
			return prefix[:len(prefix) - 1]
		}
	}
	return prefix
}
func (sr *spoukrouter) addRoute(method, path, prefix string, s SpoukHandler) {
	//nameHandler := runtime.FuncForPC(reflect.ValueOf(s).Pointer()).Name()
	//sr.spoukmux.RouteMapper[prefix + path] = spoukMapRoute{Path:prefix + path, Method:method, Handler:nameHandler}
	//prefixMiddle
	hu := sr.middlewares.getStockMiddlesPrefix(sr.fixPrefix(prefix)).wrappermidfunc(s)
	sr.router.Handle(strings.ToUpper(method), prefix + path, httprouter.Handle(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		newcarry := sr.spoukmux.getPool(w, r)
		newcarry.params = &ps
		//if sr.spoukmux.handlers != nil {
		//	sr.spoukmux.handlers.StockInit(newcarry)
		//}
		//sr.wrapperForStockInitInterface(hu)(newcarry)
		hu(newcarry)
		sr.spoukmux.putPool(newcarry)
	}))
}
func (sr *spoukrouter) checkMethod(method string) bool {
	if _, found := validMethods[method]; found {
		return true
	}
	return false
}
func (sr *spoukrouter) Multi(methods []string, path string, s SpoukHandler) {
	for _, m := range methods {
		if sr.checkMethod(m) {
			sr.addRoute(m, path, "", s)
		} else {
			err := makeErrorMessage(defRouterError, fmt.Sprintf("метод  `%s` не подходящий", m))
			log.Fatal(err.Error())
		}
	}
}
func (sr *spoukrouter) Get(path string, s SpoukHandler) {
	sr.addRoute("GET", path, "", s)
}
func (sr *spoukrouter) Post(path string, s SpoukHandler) {
	sr.addRoute("POST", path, "", s)
}
func (sr *spoukrouter) Delete(path string, s SpoukHandler) {
	sr.addRoute("DELETE", path, "", s)
}
func (sr *spoukrouter) Update(path string, s SpoukHandler) {
	sr.addRoute("UPDATE", path, "", s)
}
func (sr *spoukrouter) wrapperforSpoukHandler(h SpoukHandler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		newmux := sr.spoukmux.getPool(w, r)
		h(newmux)
		sr.spoukmux.putPool(newmux)
	})
}
//func (sr *spoukrouter) SetMethodNotAllowed(h SpoukHandler) {
//	sr.router.MethodNotAllowed = sr.wrapperforSpoukHandler(h)
//}
//---------------------------------------------------------------------------
//  SPOUKHANDLER
//---------------------------------------------------------------------------
func (m SpoukHandler) ServeHTTPSpouker(s *SpoukCarry) {
	m(s)
}
//func error404spoukhandler(s *SpoukCarry) error {
func error404spoukhandler(s *SpoukCarry) error {
	fmt.Printf("[ERROR][CODE] %v:%v\n", s.request.Method, s.request.RequestURI)
	switch s.request.Method {
	case "POST", "DELETE", "UPDATE":
		s.WriteHTML(http.StatusMethodNotAllowed, fmt.Sprintf(notallowed405, s.request.Method))
	case "GET":
		s.WriteHTML(http.StatusNotFound, fmt.Sprintf(notFound404, s.request.RequestURI))
	}
	return nil
}
func error405methodNotAllowed(s *SpoukCarry) error {
	fmt.Printf("NOT ALLOWED METHOD\n")
	s.WriteHTML(http.StatusMethodNotAllowed, notallowed405)
	return nil
}
//func error405methodNotAllowedTest() http.Handler {
//	return httprouter.Handle(func(w http.ResponseWriter, r *http.Response, ps httprouter.Params) {
//		fmt.Printf("NOT ALLOWED METHOD\n")
//		w.Write([]byte("NOT ALLOWED METHOD\n"))
//		//s.WriteHTML(http.StatusMethodNotAllowed, notallowed405)
//	})
//}

func allow405(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("NOT ALLOWED METHOD\n")
	w.Write([]byte("NOT ALLOWED METHOD\n"))
}
//func error405methodNotAllowedTest() http.Handle {
//	return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
//		fmt.Printf("NOT ALLOWED METHOD\n")
//		w.Write([]byte("NOT ALLOWED METHOD\n"))
//		//s.WriteHTML(http.StatusMethodNotAllowed, notallowed405)
//	})
//}<commit_msg>update<commit_after>package spoukfw

import (
	"github.com/julienschmidt/httprouter"
	"net/http"
	"fmt"
	"strings"
//"runtime"
//"reflect"
	"log"

"runtime"
	"reflect"
)

type (
	spoukrouter struct {
		router      httprouter.Router
		//middlewares *spoukmiddlewares
		middlewares *spoukstockmiddlewares
		spoukmux    *Spoukmux
	}
	SpoukHandler func(*SpoukCarry) error
	spoukMapRoute struct {
		Path    string
		Method  string
		Handler string
	}
)

var (
	validMethods map[string]string = map[string]string{
		"GET" : "GET",
		"POST" : "POST",
		"UPDATE": "UPDATE",
		"DELETE" : "DELETE",
	}
)

func newSpoukRouter(mux *Spoukmux) *spoukrouter {
	sp := &spoukrouter{
		spoukmux:mux,
		router:httprouter.Router{HandleMethodNotAllowed:true, RedirectFixedPath:true, RedirectTrailingSlash:true},
	}
	return sp
}
//---------------------------------------------------------------------------
//  SPOUKROUTER
//---------------------------------------------------------------------------
//     router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
func (sr *spoukrouter) StaticFiles(realpath, wwwpath string) {
	sr.router.ServeFiles(realpath, http.Dir(wwwpath))
}
func (sr spoukrouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	sr.router.ServeHTTP(w, r)
}

func (sr *spoukrouter) fixPrefix(prefix string) string {
	//убирает слеш последний заебанный
	if len(strings.TrimSpace(prefix)) == 1 && prefix == "/" {
		return ""
	}
	if len(strings.TrimSpace(prefix)) > 1 {
		if prefix[len(prefix) - 1:] == "/" {
			return prefix[:len(prefix) - 1]
		}
	}
	return prefix
}
func (sr *spoukrouter) addRoute(method, path, prefix string, s SpoukHandler) {
	nameHandler := runtime.FuncForPC(reflect.ValueOf(s).Pointer()).Name()
	sr.spoukmux.RouteMapper[prefix + path] = spoukMapRoute{Path:prefix + path, Method:method, Handler:nameHandler}
	//prefixMiddle
	hu := sr.middlewares.getStockMiddlesPrefix(sr.fixPrefix(prefix)).wrappermidfunc(s)
	sr.router.Handle(strings.ToUpper(method), prefix + path, httprouter.Handle(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		newcarry := sr.spoukmux.getPool(w, r)
		newcarry.params = &ps
		//if sr.spoukmux.handlers != nil {
		//	sr.spoukmux.handlers.StockInit(newcarry)
		//}
		//sr.wrapperForStockInitInterface(hu)(newcarry)
		hu(newcarry)
		sr.spoukmux.putPool(newcarry)
	}))
}
func (sr *spoukrouter) checkMethod(method string) bool {
	if _, found := validMethods[method]; found {
		return true
	}
	return false
}
func (sr *spoukrouter) Multi(methods []string, path string, s SpoukHandler) {
	for _, m := range methods {
		if sr.checkMethod(m) {
			sr.addRoute(m, path, "", s)
		} else {
			err := makeErrorMessage(defRouterError, fmt.Sprintf("метод  `%s` не подходящий", m))
			log.Fatal(err.Error())
		}
	}
}
func (sr *spoukrouter) Get(path string, s SpoukHandler) {
	sr.spoukmux.RouteMapper
	sr.addRoute("GET", path, "", s)
}
func (sr *spoukrouter) Post(path string, s SpoukHandler) {
	sr.addRoute("POST", path, "", s)
}
func (sr *spoukrouter) Delete(path string, s SpoukHandler) {
	sr.addRoute("DELETE", path, "", s)
}
func (sr *spoukrouter) Update(path string, s SpoukHandler) {
	sr.addRoute("UPDATE", path, "", s)
}
func (sr *spoukrouter) wrapperforSpoukHandler(h SpoukHandler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		newmux := sr.spoukmux.getPool(w, r)
		h(newmux)
		sr.spoukmux.putPool(newmux)
	})
}
//func (sr *spoukrouter) SetMethodNotAllowed(h SpoukHandler) {
//	sr.router.MethodNotAllowed = sr.wrapperforSpoukHandler(h)
//}
//---------------------------------------------------------------------------
//  SPOUKHANDLER
//---------------------------------------------------------------------------
func (m SpoukHandler) ServeHTTPSpouker(s *SpoukCarry) {
	m(s)
}
//func error404spoukhandler(s *SpoukCarry) error {
func error404spoukhandler(s *SpoukCarry) error {
	fmt.Printf("[ERROR][CODE] %v:%v\n", s.request.Method, s.request.RequestURI)
	switch s.request.Method {
	case "POST", "DELETE", "UPDATE":
		s.WriteHTML(http.StatusMethodNotAllowed, fmt.Sprintf(notallowed405, s.request.Method))
	case "GET":
		s.WriteHTML(http.StatusNotFound, fmt.Sprintf(notFound404, s.request.RequestURI))
	}
	return nil
}
func error405methodNotAllowed(s *SpoukCarry) error {
	fmt.Printf("NOT ALLOWED METHOD\n")
	s.WriteHTML(http.StatusMethodNotAllowed, notallowed405)
	return nil
}
//func error405methodNotAllowedTest() http.Handler {
//	return httprouter.Handle(func(w http.ResponseWriter, r *http.Response, ps httprouter.Params) {
//		fmt.Printf("NOT ALLOWED METHOD\n")
//		w.Write([]byte("NOT ALLOWED METHOD\n"))
//		//s.WriteHTML(http.StatusMethodNotAllowed, notallowed405)
//	})
//}

func allow405(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("NOT ALLOWED METHOD\n")
	w.Write([]byte("NOT ALLOWED METHOD\n"))
}
//func error405methodNotAllowedTest() http.Handle {
//	return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
//		fmt.Printf("NOT ALLOWED METHOD\n")
//		w.Write([]byte("NOT ALLOWED METHOD\n"))
//		//s.WriteHTML(http.StatusMethodNotAllowed, notallowed405)
//	})
//}<|endoftext|>


<commit_before>package spql

import (
	"encoding/json"
	"errors"
)

// SelectStmt holds database parsed arguments from query
type SelectStmt struct {
	Name  string `json:"name"`
	Key   string `json:"key"`
	Field string `json:"field"`
}

// ParseSelectStmt parse raw json to query.params
func ParseSelectStmt(q *Query, raw *json.RawMessage) error {
	q.Method = "Select"

	stmt := &SelectStmt{}
	json.Unmarshal(*raw, stmt)
	q.Params = stmt

	var err error

	if err := ValidateDatabaseName.MatchString(stmt.Name); !err {
		return errors.New("Invalid database name")
	}

	if err := ValidateDatabaseName.MatchString(stmt.Key); !err {
		return errors.New("Invalid key")
	}

	if stmt.Key != "" {
		if err := ValidateFieldName(stmt.Field); !err {
			return errors.New("Invalid Field")
		}
	}

	return err
}
<commit_msg>removed field from select<commit_after>package spql

import (
	"encoding/json"
	"errors"
)

// SelectStmt holds database parsed arguments from query
type SelectStmt struct {
	Name string `json:"name"`
	Key  string `json:"key"`
}

// ParseSelectStmt parse raw json to query.params
func ParseSelectStmt(q *Query, raw *json.RawMessage) error {
	q.Method = "Select"

	stmt := &SelectStmt{}
	json.Unmarshal(*raw, stmt)
	q.Params = stmt

	var err error

	if err := ValidateDatabaseName.MatchString(stmt.Name); !err {
		return errors.New("Invalid database name")
	}

	if err := ValidateDatabaseName.MatchString(stmt.Key); !err {
		return errors.New("Invalid key")
	}

	return err
}
<|endoftext|>


<commit_before>// Copyright 2015 Hajime Hoshi
//
// 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 vorbis provides Ogg/Vorbis decoder.
package vorbis

import (
	"io"
	"runtime"

	"github.com/hajimehoshi/ebiten/audio"
	"github.com/jfreymuth/oggvorbis"
)

type decoded struct {
	data       []float32
	totalBytes int
	posInBytes int
	source     io.Closer
	decoder    *oggvorbis.Reader
}

func (d *decoded) readUntil(posInBytes int) error {
	c := 0
	for len(d.data) < posInBytes/2 {
		// TODO: What if the channel is closed?
		buffer := make([]float32, 4096)
		n, err := d.decoder.Read(buffer)
		if n > 0 {
			d.data = append(d.data, buffer[:n]...)
		}
		if err == io.EOF {
			if err := d.source.Close(); err != nil {
				return err
			}
			break
		}
		if err != nil {
			return err
		}
		c++
		if c%4 == 0 {
			runtime.Gosched()
		}
	}
	return nil
}

func (d *decoded) Read(b []byte) (int, error) {
	total := d.totalBytes
	l := total - d.posInBytes
	if l > len(b) {
		l = len(b)
	}
	if l < 0 {
		return 0, io.EOF
	}
	// l must be even so that d.posInBytes is always even.
	l = l / 2 * 2
	if err := d.readUntil(d.posInBytes + l); err != nil {
		return 0, err
	}
	for i := 0; i < l/2; i++ {
		f := d.data[d.posInBytes/2+i]
		s := int16(f * (1<<15 - 1))
		b[2*i] = uint8(s)
		b[2*i+1] = uint8(s >> 8)
	}
	d.posInBytes += l
	if d.posInBytes == total {
		return l, io.EOF
	}
	return l, nil
}

func (d *decoded) Seek(offset int64, whence int) (int64, error) {
	next := int64(0)
	switch whence {
	case io.SeekStart:
		next = offset
	case io.SeekCurrent:
		next = int64(d.posInBytes) + offset
	case io.SeekEnd:
		next = int64(d.totalBytes) + offset
	}
	// pos should be always even
	next = next / 2 * 2
	d.posInBytes = int(next)
	if err := d.readUntil(d.posInBytes); err != nil {
		return 0, err
	}
	return next, nil
}

func (d *decoded) Close() error {
	runtime.SetFinalizer(d, nil)
	return nil
}

func (d *decoded) Size() int64 {
	return int64(d.totalBytes)
}

// decode accepts an ogg stream and returns a decorded stream.
func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) {
	// TODO: Lazy evaluation
	r, err := oggvorbis.NewReader(in)
	if err != nil {
		return nil, 0, 0, err
	}
	d := &decoded{
		data:       []float32{},
		totalBytes: int(r.Length()) * 4, // TODO: What if length is 0?
		posInBytes: 0,
		source:     in,
		decoder:    r,
	}
	runtime.SetFinalizer(d, (*decoded).Close)
	return d, r.Channels(), r.SampleRate(), nil
}
<commit_msg>audio/vorbis: Change the capacity of buffer<commit_after>// Copyright 2015 Hajime Hoshi
//
// 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 vorbis provides Ogg/Vorbis decoder.
package vorbis

import (
	"io"
	"runtime"

	"github.com/hajimehoshi/ebiten/audio"
	"github.com/jfreymuth/oggvorbis"
)

type decoded struct {
	data       []float32
	totalBytes int
	posInBytes int
	source     io.Closer
	decoder    *oggvorbis.Reader
}

func (d *decoded) readUntil(posInBytes int) error {
	c := 0
	for len(d.data) < posInBytes/2 {
		buffer := make([]float32, 8192)
		n, err := d.decoder.Read(buffer)
		if n > 0 {
			d.data = append(d.data, buffer[:n]...)
		}
		if err == io.EOF {
			if err := d.source.Close(); err != nil {
				return err
			}
			break
		}
		if err != nil {
			return err
		}
		c++
		if c%4 == 0 {
			runtime.Gosched()
		}
	}
	return nil
}

func (d *decoded) Read(b []byte) (int, error) {
	total := d.totalBytes
	l := total - d.posInBytes
	if l > len(b) {
		l = len(b)
	}
	if l < 0 {
		return 0, io.EOF
	}
	// l must be even so that d.posInBytes is always even.
	l = l / 2 * 2
	if err := d.readUntil(d.posInBytes + l); err != nil {
		return 0, err
	}
	for i := 0; i < l/2; i++ {
		f := d.data[d.posInBytes/2+i]
		s := int16(f * (1<<15 - 1))
		b[2*i] = uint8(s)
		b[2*i+1] = uint8(s >> 8)
	}
	d.posInBytes += l
	if d.posInBytes == total {
		return l, io.EOF
	}
	return l, nil
}

func (d *decoded) Seek(offset int64, whence int) (int64, error) {
	next := int64(0)
	switch whence {
	case io.SeekStart:
		next = offset
	case io.SeekCurrent:
		next = int64(d.posInBytes) + offset
	case io.SeekEnd:
		next = int64(d.totalBytes) + offset
	}
	// pos should be always even
	next = next / 2 * 2
	d.posInBytes = int(next)
	if err := d.readUntil(d.posInBytes); err != nil {
		return 0, err
	}
	return next, nil
}

func (d *decoded) Close() error {
	runtime.SetFinalizer(d, nil)
	return nil
}

func (d *decoded) Size() int64 {
	return int64(d.totalBytes)
}

// decode accepts an ogg stream and returns a decorded stream.
func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) {
	// TODO: Lazy evaluation
	r, err := oggvorbis.NewReader(in)
	if err != nil {
		return nil, 0, 0, err
	}
	d := &decoded{
		data:       []float32{},
		totalBytes: int(r.Length()) * 4, // TODO: What if length is 0?
		posInBytes: 0,
		source:     in,
		decoder:    r,
	}
	runtime.SetFinalizer(d, (*decoded).Close)
	return d, r.Channels(), r.SampleRate(), nil
}
<|endoftext|>


<commit_before>// Copyright 2016 CoreOS, Inc.
//
// 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 backend

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"sync"
	"testing"
	"time"
)

var (
	testMu       sync.Mutex
	testBasePort = 8080
)

func TestServer(t *testing.T) {
	testMu.Lock()
	port := testBasePort
	testBasePort++
	testMu.Unlock()

	srv, err := StartServer(port)
	if err != nil {
		t.Fatal(err)
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("getting server status update...")
	{
		resp, err := http.Get(srv.addrURL.String() + "/server-status")
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/server-status' response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("stressing node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stress",
			Endpoints: globalCluster.Endpoints(0, true),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	fmt.Println("expecting rate-limit error from node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stress",
			Endpoints: globalCluster.Endpoints(0, false),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	orig := defaultRequestInterval
	defaultRequestInterval = time.Millisecond
	defer func() {
		defaultRequestInterval = orig
	}()

	println()
	time.Sleep(time.Second)
	fmt.Println("expecting error from specifying no endpoints...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action: "stress",
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("writing to node2...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "write",
			Endpoints: globalCluster.Endpoints(1, true),
			KeyValue:  KeyValue{Key: "foo", Value: "bar"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		// hreq := &http.Request{
		// 	Method: "POST",
		// 	URL:    &tu,
		// 	Body:   ioutil.NopCloser(bytes.NewReader(data)),
		// }
		// resp, err := http.DefaultClient.Do(hreq)
		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("prefix-range from node3...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:      "get",
			RangePrefix: true,
			Endpoints:   globalCluster.Endpoints(2, true),
			KeyValue:    KeyValue{Key: "foo"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("delete-prefix from node4...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:      "delete",
			RangePrefix: true,
			Endpoints:   globalCluster.Endpoints(3, true),
			KeyValue:    KeyValue{Key: "foo"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("get from node5...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "get",
			Endpoints: globalCluster.Endpoints(4, true),
			KeyValue:  KeyValue{Key: "foo"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("stop node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stop-node",
			Endpoints: globalCluster.Endpoints(0, true),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("expecting errors after stopping node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stress",
			Endpoints: globalCluster.Endpoints(0, false),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("restart node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "restart-node",
			Endpoints: globalCluster.Endpoints(0, true),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	fmt.Println("DONE!")

	// capnslog.SetGlobalLogLevel(capnslog.CRITICAL)
	// defer capnslog.SetGlobalLogLevel(testLogLevel)
	srv.Stop()
}
<commit_msg>backend: update rate limiter for server tests<commit_after>// Copyright 2016 CoreOS, Inc.
//
// 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 backend

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"sync"
	"testing"
	"time"

	"golang.org/x/time/rate"
)

var (
	testMu       sync.Mutex
	testBasePort = 8080
)

func TestServer(t *testing.T) {
	testMu.Lock()
	port := testBasePort
	testBasePort++
	testMu.Unlock()

	srv, err := StartServer(port)
	if err != nil {
		t.Fatal(err)
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("getting server status update...")
	{
		resp, err := http.Get(srv.addrURL.String() + "/server-status")
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/server-status' response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("stressing node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stress",
			Endpoints: globalCluster.Endpoints(0, true),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	fmt.Println("expecting rate-limit error from node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stress",
			Endpoints: globalCluster.Endpoints(0, false),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	fmt.Println("allow more requests in limiter for testing")
	globalRequestLimiter.limiter.SetLimit(rate.Every(10 * time.Second))

	println()
	time.Sleep(time.Second)
	fmt.Println("expecting error from specifying no endpoints...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action: "stress",
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("writing to node2...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "write",
			Endpoints: globalCluster.Endpoints(1, true),
			KeyValue:  KeyValue{Key: "foo", Value: "bar"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		// hreq := &http.Request{
		// 	Method: "POST",
		// 	URL:    &tu,
		// 	Body:   ioutil.NopCloser(bytes.NewReader(data)),
		// }
		// resp, err := http.DefaultClient.Do(hreq)
		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("prefix-range from node3...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:      "get",
			RangePrefix: true,
			Endpoints:   globalCluster.Endpoints(2, true),
			KeyValue:    KeyValue{Key: "foo"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("delete-prefix from node4...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:      "delete",
			RangePrefix: true,
			Endpoints:   globalCluster.Endpoints(3, true),
			KeyValue:    KeyValue{Key: "foo"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("get from node5...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "get",
			Endpoints: globalCluster.Endpoints(4, true),
			KeyValue:  KeyValue{Key: "foo"},
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("stop node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stop-node",
			Endpoints: globalCluster.Endpoints(0, true),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("expecting errors after stopping node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "stress",
			Endpoints: globalCluster.Endpoints(0, false),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	println()
	time.Sleep(time.Second)
	fmt.Println("restart node1...")
	{
		tu := srv.addrURL
		tu.Path = "/client-request"

		req := ClientRequest{
			Action:    "restart-node",
			Endpoints: globalCluster.Endpoints(0, true),
		}
		data, err := json.Marshal(&req)
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("'/client-request' POST request:", string(data))

		resp, err := http.Post(tu.String(), "application/json", bytes.NewReader(data))
		if err != nil {
			t.Fatal(err)
		}
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			resp.Body.Close()
			t.Fatal(err)
		}
		resp.Body.Close()
		fmt.Println("'/client-request' POST response:", string(b))
	}

	fmt.Println("DONE!")

	// capnslog.SetGlobalLogLevel(capnslog.CRITICAL)
	// defer capnslog.SetGlobalLogLevel(testLogLevel)
	srv.Stop()
}
<|endoftext|>


<commit_before>package db

import (
    l4g "code.google.com/p/log4go"
    "database/sql"
    "domains"
    "fmt"
)

func GetFlagsForUser(uId int) ([]domains.Flag, error) {
    flags := make([]domains.Flag, 0)
    results, err := QueryRows("SELECT tag, value FROM Users u, Flags f, UsersFlags uf WHERE u.id = uf.uId AND f.id = uf.fId AND u.publicId = ?",
        Params(uId), rowToFlag)

    if err != nil {
        return flags, err
    }

    for _, result := range results {
        flag := result.(domains.Flag)
        flags = append(flags, flag)
    }

    return flags, err
}

func GetPublicFlagsForUser(uId string) ([]domains.PublicFlag, error) {
    publicFlags := make([]domains.PublicFlag, 0)
    results, err := QueryRows("SELECT value FROM Users u, Flags f, UsersFlags uf WHERE u.id = uf.uId AND f.id = uf.fId AND u.publicId = ?",
        Params(uId), rowToPublicFlag)

    if err != nil {
        return publicFlags, err
    }

    for _, result := range results {
        flag := result.(domains.PublicFlag)
        publicFlags = append(publicFlags, flag)
    }

    return publicFlags, err
}

func ValidateFlagFor(tag string, publicId string) (string, error) {

    result, err := QueryRows("SELECT id FROM Flags WHERE tag = ?", Params(tag), rowToInt)
    if err != nil {
        return "error", err
    }

    if len(result) == 0 {
        return "flag does not exist", nil
    }

    flagId := result[0].(int)

    // Get users DB id
    result, err = QueryRows("SELECT id FROM Users WHERE publicId = ?", Params(publicId), rowToInt)
    if err != nil {
        return "error", err
    }
    if len(result) == 0 { // User not found
        return "user does not exist", nil
    }

    userId := result[0].(int)

    // Check if it was already validated
    result, err = QueryRows("SELECT uId FROM UsersFlags WHERE fId = ? AND uId = ?", Params(flagId, userId), rowToInt)
    if err != nil {
        return "error", err
    }
    if len(result) != 0 { // Flag found
        return "flag already validated", nil
    }

    // Set the flag as found
    _, err = Insert("INSERT UsersFlags (uId, fId) VALUES (?, ?) ", Params(userId, flagId))
    if err != nil {
        errMsg := fmt.Sprintf("Failed INSERT to UsersFlags for USER %d and FLAG %d", userId, flagId)
        l4g.Error(errMsg)
        return "flag not validated", err
    }

    return "flag validated", nil
}

func rowToFlag(rows *sql.Rows) (interface{}, error) {
    flag := domains.Flag{}
    err := rows.Scan(&flag.Tag, &flag.Value)
    return flag, err
}

func rowToPublicFlag(rows *sql.Rows) (interface{}, error) {
    flag := domains.PublicFlag{}
    err := rows.Scan(&flag.Value)
    return flag, err
}

func rowToInt(rows *sql.Rows) (interface{}, error) {
    var r int
    err := rows.Scan(&r)
    return r, err
}
<commit_msg>case sensitive flag hash comparisons<commit_after>package db

import (
    l4g "code.google.com/p/log4go"
    "database/sql"
    "domains"
    "fmt"
)

func GetFlagsForUser(uId int) ([]domains.Flag, error) {
    flags := make([]domains.Flag, 0)
    results, err := QueryRows("SELECT tag, value FROM Users u, Flags f, UsersFlags uf WHERE u.id = uf.uId AND f.id = uf.fId AND u.publicId = ?",
        Params(uId), rowToFlag)

    if err != nil {
        return flags, err
    }

    for _, result := range results {
        flag := result.(domains.Flag)
        flags = append(flags, flag)
    }

    return flags, err
}

func GetPublicFlagsForUser(uId string) ([]domains.PublicFlag, error) {
    publicFlags := make([]domains.PublicFlag, 0)
    results, err := QueryRows("SELECT value FROM Users u, Flags f, UsersFlags uf WHERE u.id = uf.uId AND f.id = uf.fId AND u.publicId = ?",
        Params(uId), rowToPublicFlag)

    if err != nil {
        return publicFlags, err
    }

    for _, result := range results {
        flag := result.(domains.PublicFlag)
        publicFlags = append(publicFlags, flag)
    }

    return publicFlags, err
}

func ValidateFlagFor(tag string, publicId string) (string, error) {

    result, err := QueryRows("SELECT id FROM Flags WHERE tag = BINARY ?", Params(tag), rowToInt)
    if err != nil {
        return "error", err
    }

    if len(result) == 0 {
        return "flag does not exist", nil
    }

    flagId := result[0].(int)

    // Get users DB id
    result, err = QueryRows("SELECT id FROM Users WHERE publicId = ?", Params(publicId), rowToInt)
    if err != nil {
        return "error", err
    }
    if len(result) == 0 { // User not found
        return "user does not exist", nil
    }

    userId := result[0].(int)

    // Check if it was already validated
    result, err = QueryRows("SELECT uId FROM UsersFlags WHERE fId = ? AND uId = ?", Params(flagId, userId), rowToInt)
    if err != nil {
        return "error", err
    }
    if len(result) != 0 { // Flag found
        return "flag already validated", nil
    }

    // Set the flag as found
    _, err = Insert("INSERT UsersFlags (uId, fId) VALUES (?, ?) ", Params(userId, flagId))
    if err != nil {
        errMsg := fmt.Sprintf("Failed INSERT to UsersFlags for USER %d and FLAG %d", userId, flagId)
        l4g.Error(errMsg)
        return "flag not validated", err
    }

    return "flag validated", nil
}

func rowToFlag(rows *sql.Rows) (interface{}, error) {
    flag := domains.Flag{}
    err := rows.Scan(&flag.Tag, &flag.Value)
    return flag, err
}

func rowToPublicFlag(rows *sql.Rows) (interface{}, error) {
    flag := domains.PublicFlag{}
    err := rows.Scan(&flag.Value)
    return flag, err
}

func rowToInt(rows *sql.Rows) (interface{}, error) {
    var r int
    err := rows.Scan(&r)
    return r, err
}
<|endoftext|>


<commit_before>package main_test

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/url"
	"os"
	"path"
	"strings"

	"google.golang.org/grpc/grpclog"

	"code.cloudfoundry.org/bbs"
	bbsconfig "code.cloudfoundry.org/bbs/cmd/bbs/config"
	bbstestrunner "code.cloudfoundry.org/bbs/cmd/bbs/testrunner"
	"code.cloudfoundry.org/bbs/encryption"
	"code.cloudfoundry.org/bbs/models"
	"code.cloudfoundry.org/bbs/test_helpers"
	"code.cloudfoundry.org/bbs/test_helpers/sqlrunner"
	"code.cloudfoundry.org/consuladapter"
	"code.cloudfoundry.org/consuladapter/consulrunner"
	"code.cloudfoundry.org/durationjson"
	"code.cloudfoundry.org/inigo/helpers/portauthority"
	"code.cloudfoundry.org/lager"
	"code.cloudfoundry.org/lager/lagerflags"
	"code.cloudfoundry.org/lager/lagertest"
	"code.cloudfoundry.org/locket"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/ginkgomon"

	"testing"
	"time"
)

var (
	auctioneerPath       string
	auctioneerServerPort uint16
	auctioneerLocation   string

	dotNetStack     = "dot-net"
	dotNetRootFSURL = models.PreloadedRootFS(dotNetStack)
	linuxStack      = "linux"
	linuxRootFSURL  = models.PreloadedRootFS(linuxStack)

	consulRunner *consulrunner.ClusterRunner
	consulClient consuladapter.Client

	bbsConfig  bbsconfig.BBSConfig
	bbsBinPath string
	bbsURL     *url.URL
	bbsRunner  *ginkgomon.Runner
	bbsProcess ifrit.Process
	bbsClient  bbs.InternalClient

	locketBinPath string

	sqlProcess ifrit.Process
	sqlRunner  sqlrunner.SQLRunner

	logger        lager.Logger
	portAllocator portauthority.PortAllocator
)

func TestAuctioneer(t *testing.T) {
	// these integration tests can take a bit, especially under load;
	// 1 second is too harsh
	SetDefaultEventuallyTimeout(10 * time.Second)

	RegisterFailHandler(Fail)
	RunSpecs(t, "Auctioneer Cmd Suite")
}

var _ = SynchronizedBeforeSuite(func() []byte {
	compiledBBSPath, err := gexec.Build("code.cloudfoundry.org/bbs/cmd/bbs", "-race")
	Expect(err).NotTo(HaveOccurred())

	compiledAuctioneerPath, err := gexec.Build("code.cloudfoundry.org/auctioneer/cmd/auctioneer", "-race")
	Expect(err).NotTo(HaveOccurred())

	locketPath, err := gexec.Build("code.cloudfoundry.org/locket/cmd/locket", "-race")
	Expect(err).NotTo(HaveOccurred())

	return []byte(strings.Join([]string{compiledAuctioneerPath, compiledBBSPath, locketPath}, ","))
}, func(pathsByte []byte) {
	grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
	node := GinkgoParallelNode()
	startPort := 1050 * node
	portRange := 1000
	endPort := startPort + portRange

	var err error
	portAllocator, err = portauthority.New(startPort, endPort)
	Expect(err).NotTo(HaveOccurred())

	paths := string(pathsByte)
	auctioneerPath = strings.Split(paths, ",")[0]
	bbsBinPath = strings.Split(paths, ",")[1]
	locketBinPath = strings.Split(paths, ",")[2]

	dbName := fmt.Sprintf("diego_%d", GinkgoParallelNode())
	sqlRunner = test_helpers.NewSQLRunner(dbName)
	sqlProcess = ginkgomon.Invoke(sqlRunner)

	consulStartingPort, err := portAllocator.ClaimPorts(consulrunner.PortOffsetLength)
	Expect(err).NotTo(HaveOccurred())
	consulRunner = consulrunner.NewClusterRunner(
		consulrunner.ClusterRunnerConfig{
			StartingPort: int(consulStartingPort),
			NumNodes:     1,
			Scheme:       "http",
		},
	)

	auctioneerServerPort, err = portAllocator.ClaimPorts(1)
	Expect(err).NotTo(HaveOccurred())
	auctioneerLocation = fmt.Sprintf("127.0.0.1:%d", auctioneerServerPort)

	logger = lagertest.NewTestLogger("test")

	consulRunner.Start()
	consulRunner.WaitUntilReady()

	bbsPort, err := portAllocator.ClaimPorts(1)
	Expect(err).NotTo(HaveOccurred())
	healthPort, err := portAllocator.ClaimPorts(1)
	Expect(err).NotTo(HaveOccurred())
	bbsAddress := fmt.Sprintf("127.0.0.1:%d", bbsPort)
	healthAddress := fmt.Sprintf("127.0.0.1:%d", healthPort)

	bbsURL = &url.URL{
		Scheme: "https",
		Host:   bbsAddress,
	}

	fixturesPath := path.Join(os.Getenv("GOPATH"), "src/code.cloudfoundry.org/auctioneer/cmd/auctioneer/fixtures")

	bbsConfig = bbsconfig.BBSConfig{
		SessionName:                     "bbs",
		CommunicationTimeout:            durationjson.Duration(10 * time.Second),
		RequireSSL:                      true,
		DesiredLRPCreationTimeout:       durationjson.Duration(1 * time.Minute),
		ExpireCompletedTaskDuration:     durationjson.Duration(2 * time.Minute),
		ExpirePendingTaskDuration:       durationjson.Duration(30 * time.Minute),
		EnableConsulServiceRegistration: false,
		ConvergeRepeatInterval:          durationjson.Duration(30 * time.Second),
		KickTaskDuration:                durationjson.Duration(30 * time.Second),
		LockTTL:                         durationjson.Duration(locket.DefaultSessionTTL),
		LockRetryInterval:               durationjson.Duration(locket.RetryInterval),
		ReportInterval:                  durationjson.Duration(1 * time.Minute),
		ConvergenceWorkers:              20,
		UpdateWorkers:                   1000,
		TaskCallbackWorkers:             1000,
		MaxOpenDatabaseConnections:      200,
		MaxIdleDatabaseConnections:      200,
		AuctioneerRequireTLS:            false,
		RepClientSessionCacheSize:       0,
		RepRequireTLS:                   false,
		LagerConfig:                     lagerflags.DefaultLagerConfig(),
		GenerateSuspectActualLRPs:       false,

		ListenAddress:                  bbsAddress,
		AdvertiseURL:                   bbsURL.String(),
		AuctioneerAddress:              "http://" + auctioneerLocation,
		ConsulCluster:                  consulRunner.ConsulCluster(),
		HealthAddress:                  healthAddress,
		LocksLocketEnabled:             false,
		CellRegistrationsLocketEnabled: false,
		EncryptionConfig: encryption.EncryptionConfig{
			EncryptionKeys: map[string]string{"label": "key"},
			ActiveKeyLabel: "label",
		},

		DatabaseDriver:                sqlRunner.DriverName(),
		DatabaseConnectionString:      sqlRunner.ConnectionString(),
		DetectConsulCellRegistrations: true,

		CaFile:   path.Join(fixturesPath, "green-certs", "ca.crt"),
		CertFile: path.Join(fixturesPath, "green-certs", "server.crt"),
		KeyFile:  path.Join(fixturesPath, "green-certs", "server.key"),
	}
})

var _ = BeforeEach(func() {
	consulRunner.Reset()

	bbsRunner = bbstestrunner.New(bbsBinPath, bbsConfig)
	bbsProcess = ginkgomon.Invoke(bbsRunner)

	consulClient = consulRunner.NewClient()
})

var _ = AfterEach(func() {
	ginkgomon.Kill(bbsProcess)

	sqlRunner.Reset()
})

var _ = SynchronizedAfterSuite(func() {
	if consulRunner != nil {
		consulRunner.Stop()
	}

	ginkgomon.Kill(sqlProcess)
}, func() {
	gexec.CleanupBuildArtifacts()
})
<commit_msg>remove GenerateSuspectActualLRPs<commit_after>package main_test

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/url"
	"os"
	"path"
	"strings"

	"google.golang.org/grpc/grpclog"

	"code.cloudfoundry.org/bbs"
	bbsconfig "code.cloudfoundry.org/bbs/cmd/bbs/config"
	bbstestrunner "code.cloudfoundry.org/bbs/cmd/bbs/testrunner"
	"code.cloudfoundry.org/bbs/encryption"
	"code.cloudfoundry.org/bbs/models"
	"code.cloudfoundry.org/bbs/test_helpers"
	"code.cloudfoundry.org/bbs/test_helpers/sqlrunner"
	"code.cloudfoundry.org/consuladapter"
	"code.cloudfoundry.org/consuladapter/consulrunner"
	"code.cloudfoundry.org/durationjson"
	"code.cloudfoundry.org/inigo/helpers/portauthority"
	"code.cloudfoundry.org/lager"
	"code.cloudfoundry.org/lager/lagerflags"
	"code.cloudfoundry.org/lager/lagertest"
	"code.cloudfoundry.org/locket"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/ginkgomon"

	"testing"
	"time"
)

var (
	auctioneerPath       string
	auctioneerServerPort uint16
	auctioneerLocation   string

	dotNetStack     = "dot-net"
	dotNetRootFSURL = models.PreloadedRootFS(dotNetStack)
	linuxStack      = "linux"
	linuxRootFSURL  = models.PreloadedRootFS(linuxStack)

	consulRunner *consulrunner.ClusterRunner
	consulClient consuladapter.Client

	bbsConfig  bbsconfig.BBSConfig
	bbsBinPath string
	bbsURL     *url.URL
	bbsRunner  *ginkgomon.Runner
	bbsProcess ifrit.Process
	bbsClient  bbs.InternalClient

	locketBinPath string

	sqlProcess ifrit.Process
	sqlRunner  sqlrunner.SQLRunner

	logger        lager.Logger
	portAllocator portauthority.PortAllocator
)

func TestAuctioneer(t *testing.T) {
	// these integration tests can take a bit, especially under load;
	// 1 second is too harsh
	SetDefaultEventuallyTimeout(10 * time.Second)

	RegisterFailHandler(Fail)
	RunSpecs(t, "Auctioneer Cmd Suite")
}

var _ = SynchronizedBeforeSuite(func() []byte {
	compiledBBSPath, err := gexec.Build("code.cloudfoundry.org/bbs/cmd/bbs", "-race")
	Expect(err).NotTo(HaveOccurred())

	compiledAuctioneerPath, err := gexec.Build("code.cloudfoundry.org/auctioneer/cmd/auctioneer", "-race")
	Expect(err).NotTo(HaveOccurred())

	locketPath, err := gexec.Build("code.cloudfoundry.org/locket/cmd/locket", "-race")
	Expect(err).NotTo(HaveOccurred())

	return []byte(strings.Join([]string{compiledAuctioneerPath, compiledBBSPath, locketPath}, ","))
}, func(pathsByte []byte) {
	grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
	node := GinkgoParallelNode()
	startPort := 1050 * node
	portRange := 1000
	endPort := startPort + portRange

	var err error
	portAllocator, err = portauthority.New(startPort, endPort)
	Expect(err).NotTo(HaveOccurred())

	paths := string(pathsByte)
	auctioneerPath = strings.Split(paths, ",")[0]
	bbsBinPath = strings.Split(paths, ",")[1]
	locketBinPath = strings.Split(paths, ",")[2]

	dbName := fmt.Sprintf("diego_%d", GinkgoParallelNode())
	sqlRunner = test_helpers.NewSQLRunner(dbName)
	sqlProcess = ginkgomon.Invoke(sqlRunner)

	consulStartingPort, err := portAllocator.ClaimPorts(consulrunner.PortOffsetLength)
	Expect(err).NotTo(HaveOccurred())
	consulRunner = consulrunner.NewClusterRunner(
		consulrunner.ClusterRunnerConfig{
			StartingPort: int(consulStartingPort),
			NumNodes:     1,
			Scheme:       "http",
		},
	)

	auctioneerServerPort, err = portAllocator.ClaimPorts(1)
	Expect(err).NotTo(HaveOccurred())
	auctioneerLocation = fmt.Sprintf("127.0.0.1:%d", auctioneerServerPort)

	logger = lagertest.NewTestLogger("test")

	consulRunner.Start()
	consulRunner.WaitUntilReady()

	bbsPort, err := portAllocator.ClaimPorts(1)
	Expect(err).NotTo(HaveOccurred())
	healthPort, err := portAllocator.ClaimPorts(1)
	Expect(err).NotTo(HaveOccurred())
	bbsAddress := fmt.Sprintf("127.0.0.1:%d", bbsPort)
	healthAddress := fmt.Sprintf("127.0.0.1:%d", healthPort)

	bbsURL = &url.URL{
		Scheme: "https",
		Host:   bbsAddress,
	}

	fixturesPath := path.Join(os.Getenv("GOPATH"), "src/code.cloudfoundry.org/auctioneer/cmd/auctioneer/fixtures")

	bbsConfig = bbsconfig.BBSConfig{
		SessionName:                     "bbs",
		CommunicationTimeout:            durationjson.Duration(10 * time.Second),
		RequireSSL:                      true,
		DesiredLRPCreationTimeout:       durationjson.Duration(1 * time.Minute),
		ExpireCompletedTaskDuration:     durationjson.Duration(2 * time.Minute),
		ExpirePendingTaskDuration:       durationjson.Duration(30 * time.Minute),
		EnableConsulServiceRegistration: false,
		ConvergeRepeatInterval:          durationjson.Duration(30 * time.Second),
		KickTaskDuration:                durationjson.Duration(30 * time.Second),
		LockTTL:                         durationjson.Duration(locket.DefaultSessionTTL),
		LockRetryInterval:               durationjson.Duration(locket.RetryInterval),
		ReportInterval:                  durationjson.Duration(1 * time.Minute),
		ConvergenceWorkers:              20,
		UpdateWorkers:                   1000,
		TaskCallbackWorkers:             1000,
		MaxOpenDatabaseConnections:      200,
		MaxIdleDatabaseConnections:      200,
		AuctioneerRequireTLS:            false,
		RepClientSessionCacheSize:       0,
		RepRequireTLS:                   false,
		LagerConfig:                     lagerflags.DefaultLagerConfig(),

		ListenAddress:                  bbsAddress,
		AdvertiseURL:                   bbsURL.String(),
		AuctioneerAddress:              "http://" + auctioneerLocation,
		ConsulCluster:                  consulRunner.ConsulCluster(),
		HealthAddress:                  healthAddress,
		LocksLocketEnabled:             false,
		CellRegistrationsLocketEnabled: false,
		EncryptionConfig: encryption.EncryptionConfig{
			EncryptionKeys: map[string]string{"label": "key"},
			ActiveKeyLabel: "label",
		},

		DatabaseDriver:                sqlRunner.DriverName(),
		DatabaseConnectionString:      sqlRunner.ConnectionString(),
		DetectConsulCellRegistrations: true,

		CaFile:   path.Join(fixturesPath, "green-certs", "ca.crt"),
		CertFile: path.Join(fixturesPath, "green-certs", "server.crt"),
		KeyFile:  path.Join(fixturesPath, "green-certs", "server.key"),
	}
})

var _ = BeforeEach(func() {
	consulRunner.Reset()

	bbsRunner = bbstestrunner.New(bbsBinPath, bbsConfig)
	bbsProcess = ginkgomon.Invoke(bbsRunner)

	consulClient = consulRunner.NewClient()
})

var _ = AfterEach(func() {
	ginkgomon.Kill(bbsProcess)

	sqlRunner.Reset()
})

var _ = SynchronizedAfterSuite(func() {
	if consulRunner != nil {
		consulRunner.Stop()
	}

	ginkgomon.Kill(sqlProcess)
}, func() {
	gexec.CleanupBuildArtifacts()
})
<|endoftext|>


<commit_before>// Package hypergo is a client for HyperDex.
package hyperdex

/*
#cgo LDFLAGS: -lhyperdex-client -lhyperdex-admin
#include <netinet/in.h>
#include "hyperdex/client.h"
#include "hyperdex/admin.h"
#include "hyperdex/datastructures.h"
*/
import "C"

import (
	"fmt"
	"io"
	"log"
	"runtime"
)

// CHANNEL_BUFFER_SIZE is the size of all the returned channels' buffer.
// You can set it to 0 if you want unbuffered channels.
var CHANNEL_BUFFER_SIZE = 1

// Timeout in miliseconds.
// Negative timeout means no timeout.
var TIMEOUT = -1

// Admin is a priviledged client used to do meta operations to hyperdex
type Admin struct {
	ptr       *C.struct_hyperdex_admin
	requests  []adminRequest
	closeChan chan bool
}

// A hyperdex object.
// Err contains any error that happened when trying to retrieve
// this object.
type Object struct {
	Err   error
	Key   string
	Attrs Attributes
}

// Read-only channel of objects
type ObjectChannel <-chan Object

// Read-only channel of errors
type ErrorChannel <-chan error

// Correspond to a hyperdex_client_attribute_check
type Condition struct {
	Attr      string
	Value     interface{}
	Predicate int
}

func NewAdmin(ip string, port int) (*Admin, error) {
	C_admin := C.hyperdex_admin_create(C.CString(ip), C.uint16_t(port))

	if C_admin == nil {
		return nil, fmt.Errorf("Could not create hyperdex_admin (ip=%s, port=%d)", ip, port)
	}

	admin := &Admin{
		C_admin,
		make([]adminRequest, 0, 8),
		make(chan bool, 1),
	}

	go func() {
		for {
			select {
			// quit goroutine when client is destroyed
			case <-admin.closeChan:
				return
			default:
				// check if there are pending requests
				// and only if there are, call hyperdex_client_loop
				if len(admin.requests) > 0 {
					var status C.enum_hyperdex_admin_returncode
					ret := int64(C.hyperdex_admin_loop(admin.ptr, C.int(TIMEOUT), &status))
					//log.Printf("hyperdex_client_loop(%X, %d, %X) -> %d\n", unsafe.Pointer(client.ptr), hyperdex_client_loop_timeout, unsafe.Pointer(&status), ret)
					if ret < 0 {
						panic(newInternalError(status, "Admin error"))
					}
					// find processed request among pending requests
					for i, req := range admin.requests {
						if req.id == ret {
							if status == C.HYPERDEX_ADMIN_SUCCESS {
								switch *req.status {
								case C.HYPERDEX_ADMIN_SUCCESS:
									if req.success != nil {
										req.success()
									}
								default:
									if req.failure != nil {
										req.failure(*req.status,
											C.GoString(C.hyperdex_admin_error_message(admin.ptr)))
									}
								}
							} else if req.failure != nil {
								req.failure(status,
									C.GoString(C.hyperdex_admin_error_message(admin.ptr)))
							}
							admin.requests = append(admin.requests[:i], admin.requests[i+1:]...)
						}
					}
				}
				// prevent other goroutines from starving
				runtime.Gosched()
			}
		}
		panic("Should not be reached: end of infinite loop")
	}()

	return admin, nil
}

// Destroy closes the connection between the Admin and hyperdex. It has to be used on a admin that is not used anymore.
//
// For every call to NewAdmin, there must be a call to Destroy.
func (admin *Admin) Destroy() {
	close(admin.closeChan)
	C.hyperdex_admin_destroy(admin.ptr)
}
<commit_msg>Make code more readable, remove some dead code and have goimports fix the imports<commit_after>// Package hypergo is a client for HyperDex.
package hyperdex

/*
#cgo LDFLAGS: -lhyperdex-client -lhyperdex-admin
#include <netinet/in.h>
#include "hyperdex/client.h"
#include "hyperdex/admin.h"
#include "hyperdex/datastructures.h"
*/
import "C"

import (
	"fmt"
	"runtime"
)

// CHANNEL_BUFFER_SIZE is the size of all the returned channels' buffer.
// You can set it to 0 if you want unbuffered channels.
var CHANNEL_BUFFER_SIZE = 1

// Timeout in miliseconds.
// Negative timeout means no timeout.
var TIMEOUT = -1

// Admin is a priviledged client used to do meta operations to hyperdex
type Admin struct {
	ptr       *C.struct_hyperdex_admin
	requests  []adminRequest
	closeChan chan bool
}

// A hyperdex object.
// Err contains any error that happened when trying to retrieve
// this object.
type Object struct {
	Err   error
	Key   string
	Attrs Attributes
}

// Read-only channel of objects
type ObjectChannel <-chan Object

// Read-only channel of errors
type ErrorChannel <-chan error

// Correspond to a hyperdex_client_attribute_check
type Condition struct {
	Attr      string
	Value     interface{}
	Predicate int
}

func NewAdmin(ip string, port int) (*Admin, error) {
	C_admin := C.hyperdex_admin_create(C.CString(ip), C.uint16_t(port))

	if C_admin == nil {
		return nil, fmt.Errorf("Could not create hyperdex_admin (ip=%s, port=%d)", ip, port)
	}

	admin := &Admin{
		C_admin,
		make([]adminRequest, 0, 8),
		make(chan bool, 1),
	}

	go func() {
		for {
			select {
			// quit goroutine when client is destroyed
			case <-admin.closeChan:
				return
			default:
				// check if there are pending requests
				// and only if there are, call hyperdex_client_loop
				if len(admin.requests) == 0 {
					continue
				}

				var status C.enum_hyperdex_admin_returncode
				ret := int64(C.hyperdex_admin_loop(admin.ptr, C.int(TIMEOUT), &status))
				if ret < 0 {
					panic(newInternalError(status, "Admin error"))
				}

				// find processed request among pending requests
				for i, req := range admin.requests {
					if req.id != ret {
						continue
					}

					if status == C.HYPERDEX_ADMIN_SUCCESS {
						switch *req.status {
						case C.HYPERDEX_ADMIN_SUCCESS:
							if req.success != nil {
								req.success()
							}
						default:
							if req.failure != nil {
								req.failure(*req.status,
									C.GoString(C.hyperdex_admin_error_message(admin.ptr)))
							}
						}
					} else if req.failure != nil {
						req.failure(status,
							C.GoString(C.hyperdex_admin_error_message(admin.ptr)))
					}
					admin.requests = append(admin.requests[:i], admin.requests[i+1:]...)
				}
			}
			// prevent other goroutines from starving
			runtime.Gosched()
		}
		panic("Should not be reached: end of infinite loop")
	}()

	return admin, nil
}

// Destroy closes the connection between the Admin and hyperdex. It has to be used on a admin that is not used anymore.
//
// For every call to NewAdmin, there must be a call to Destroy.
func (admin *Admin) Destroy() {
	close(admin.closeChan)
	C.hyperdex_admin_destroy(admin.ptr)
}
<|endoftext|>


<commit_before>package boilingcore

import (
	"fmt"

	"github.com/volatiletech/sqlboiler/drivers"
	"github.com/volatiletech/sqlboiler/strmangle"
)

// Aliases defines aliases for the generation run
type Aliases struct {
	Tables map[string]TableAlias `toml:"tables,omitempty" json:"tables,omitempty"`
}

// TableAlias defines the spellings for a table name in Go
type TableAlias struct {
	UpPlural     string `toml:"up_plural,omitempty" json:"up_plural,omitempty"`
	UpSingular   string `toml:"up_singular,omitempty" json:"up_singular,omitempty"`
	DownPlural   string `toml:"down_plural,omitempty" json:"down_plural,omitempty"`
	DownSingular string `toml:"down_singular,omitempty" json:"down_singular,omitempty"`

	Columns       map[string]string            `toml:"columns,omitempty" json:"columns,omitempty"`
	Relationships map[string]RelationshipAlias `toml:"relationships,omitempty" json:"relationships,omitempty"`
}

// RelationshipAlias defines the naming for both sides of
// a foreign key.
type RelationshipAlias struct {
	Local   string `toml:"local,omitempty" json:"local,omitempty"`
	Foreign string `toml:"foreign,omitempty" json:"foreign,omitempty"`
}

// FillAliases takes the table information from the driver
// and fills in aliases where the user has provided none.
//
// This leaves us with a complete list of Go names for all tables,
// columns, and relationships.
func FillAliases(a *Aliases, tables []drivers.Table) {
	if a.Tables == nil {
		a.Tables = make(map[string]TableAlias)
	}

	for _, t := range tables {
		if t.IsJoinTable {
			if _, ok := a.Tables[t.Name]; !ok {
				a.Tables[t.Name] = TableAlias{Relationships: make(map[string]RelationshipAlias)}
			}
			continue
		}

		table := a.Tables[t.Name]

		if len(table.UpPlural) == 0 {
			table.UpPlural = strmangle.TitleCase(strmangle.Plural(t.Name))
		}
		if len(table.UpSingular) == 0 {
			table.UpSingular = strmangle.TitleCase(strmangle.Singular(t.Name))
		}
		if len(table.DownPlural) == 0 {
			table.DownPlural = strmangle.CamelCase(strmangle.Plural(t.Name))
		}
		if len(table.DownSingular) == 0 {
			table.DownSingular = strmangle.CamelCase(strmangle.Singular(t.Name))
		}

		if table.Columns == nil {
			table.Columns = make(map[string]string)
		}
		if table.Relationships == nil {
			table.Relationships = make(map[string]RelationshipAlias)
		}

		for _, c := range t.Columns {
			if _, ok := table.Columns[c.Name]; !ok {
				table.Columns[c.Name] = strmangle.TitleCase(c.Name)
			}
		}

		a.Tables[t.Name] = table

		for _, k := range t.FKeys {
			r := table.Relationships[k.Name]
			if len(r.Local) != 0 && len(r.Foreign) != 0 {
				continue
			}

			local, foreign := txtNameToOne(k)
			if len(r.Local) == 0 {
				r.Local = local
			}
			if len(r.Foreign) == 0 {
				r.Foreign = foreign
			}

			table.Relationships[k.Name] = r
		}

	}

	for _, t := range tables {
		if !t.IsJoinTable {
			continue
		}

		table := a.Tables[t.Name]

		lhs := t.FKeys[0]
		rhs := t.FKeys[1]

		lhsAlias, lhsOK := table.Relationships[lhs.Name]
		rhsAlias, rhsOK := table.Relationships[rhs.Name]

		if lhsOK && len(lhsAlias.Local) != 0 && len(lhsAlias.Foreign) != 0 &&
			rhsOK && len(rhsAlias.Local) != 0 && len(rhsAlias.Foreign) != 0 {
			continue
		}

		// Here we actually reverse the meaning of local/foreign to be
		// consistent with the way normal one-to-many relationships are done.
		// That's to say local = the side with the foreign key. Now in a many-to-many
		// if we were able to not have a join table our foreign key say "videos_id"
		// would be on the tags table. Hence the relationships should look like:
		// videos_tags.relationships.fk_video_id.local   = "Tags"
		// videos_tags.relationships.fk_video_id.foreign = "Videos"
		// Consistent, yes. Confusing? Also yes.

		lhsName, rhsName := txtNameToMany(lhs, rhs)

		if len(lhsAlias.Local) != 0 {
			rhsName = lhsAlias.Local
		} else if len(rhsAlias.Local) != 0 {
			lhsName = rhsAlias.Local
		}

		if len(lhsAlias.Foreign) != 0 {
			lhsName = lhsAlias.Foreign
		} else if len(rhsAlias.Foreign) != 0 {
			rhsName = rhsAlias.Foreign
		}

		if len(lhsAlias.Local) == 0 {
			lhsAlias.Local = rhsName
		}
		if len(lhsAlias.Foreign) == 0 {
			lhsAlias.Foreign = lhsName
		}
		if len(rhsAlias.Local) == 0 {
			rhsAlias.Local = lhsName
		}
		if len(rhsAlias.Foreign) == 0 {
			rhsAlias.Foreign = rhsName
		}

		table.Relationships[lhs.Name] = lhsAlias
		table.Relationships[rhs.Name] = rhsAlias
	}
}

// Table gets a table alias, panics if not found.
func (a Aliases) Table(table string) TableAlias {
	t, ok := a.Tables[table]
	if !ok {
		panic("could not find table aliases for: " + table)
	}

	return t
}

// Column get's a column's aliased name, panics if not found.
func (t TableAlias) Column(column string) string {
	c, ok := t.Columns[column]
	if !ok {
		panic(fmt.Sprintf("could not find column alias for: %s.%s", t.UpSingular, column))
	}

	return c
}

// Relationship looks up a relationship, panics if not found.
func (t TableAlias) Relationship(fkey string) RelationshipAlias {
	r, ok := t.Relationships[fkey]
	if !ok {
		panic(fmt.Sprintf("could not find relationship alias for: %s.%s", t.UpSingular, fkey))
	}

	return r
}

// ManyRelationship looks up a relationship alias, panics if not found.
// It will first try to look up a join table relationship, then it will
// try a normal one-to-many relationship. That's to say joinTable/joinTableFKey
// are used if they're not empty.
//
// This allows us to skip additional conditionals in the templates.
func (a Aliases) ManyRelationship(table, fkey, joinTable, joinTableFKey string) RelationshipAlias {
	var lookupTable, lookupFKey string
	if len(joinTable) != 0 {
		lookupTable, lookupFKey = joinTable, joinTableFKey
	} else {
		lookupTable, lookupFKey = table, fkey
	}

	t := a.Table(lookupTable)
	return t.Relationship(lookupFKey)
}
<commit_msg>Fix: `panic: assignment to entry in nil map`<commit_after>package boilingcore

import (
	"fmt"

	"github.com/volatiletech/sqlboiler/drivers"
	"github.com/volatiletech/sqlboiler/strmangle"
)

// Aliases defines aliases for the generation run
type Aliases struct {
	Tables map[string]TableAlias `toml:"tables,omitempty" json:"tables,omitempty"`
}

// TableAlias defines the spellings for a table name in Go
type TableAlias struct {
	UpPlural     string `toml:"up_plural,omitempty" json:"up_plural,omitempty"`
	UpSingular   string `toml:"up_singular,omitempty" json:"up_singular,omitempty"`
	DownPlural   string `toml:"down_plural,omitempty" json:"down_plural,omitempty"`
	DownSingular string `toml:"down_singular,omitempty" json:"down_singular,omitempty"`

	Columns       map[string]string            `toml:"columns,omitempty" json:"columns,omitempty"`
	Relationships map[string]RelationshipAlias `toml:"relationships,omitempty" json:"relationships,omitempty"`
}

// RelationshipAlias defines the naming for both sides of
// a foreign key.
type RelationshipAlias struct {
	Local   string `toml:"local,omitempty" json:"local,omitempty"`
	Foreign string `toml:"foreign,omitempty" json:"foreign,omitempty"`
}

// FillAliases takes the table information from the driver
// and fills in aliases where the user has provided none.
//
// This leaves us with a complete list of Go names for all tables,
// columns, and relationships.
func FillAliases(a *Aliases, tables []drivers.Table) {
	if a.Tables == nil {
		a.Tables = make(map[string]TableAlias)
	}

	for _, t := range tables {
		if t.IsJoinTable {
			if _, ok := a.Tables[t.Name]; !ok  || a.Tables[t.Name].Relationships == nil {
				a.Tables[t.Name] = TableAlias{Relationships: make(map[string]RelationshipAlias)}
			}
			continue
		}

		table := a.Tables[t.Name]

		if len(table.UpPlural) == 0 {
			table.UpPlural = strmangle.TitleCase(strmangle.Plural(t.Name))
		}
		if len(table.UpSingular) == 0 {
			table.UpSingular = strmangle.TitleCase(strmangle.Singular(t.Name))
		}
		if len(table.DownPlural) == 0 {
			table.DownPlural = strmangle.CamelCase(strmangle.Plural(t.Name))
		}
		if len(table.DownSingular) == 0 {
			table.DownSingular = strmangle.CamelCase(strmangle.Singular(t.Name))
		}

		if table.Columns == nil {
			table.Columns = make(map[string]string)
		}
		if table.Relationships == nil {
			table.Relationships = make(map[string]RelationshipAlias)
		}

		for _, c := range t.Columns {
			if _, ok := table.Columns[c.Name]; !ok {
				table.Columns[c.Name] = strmangle.TitleCase(c.Name)
			}
		}

		a.Tables[t.Name] = table

		for _, k := range t.FKeys {
			r := table.Relationships[k.Name]
			if len(r.Local) != 0 && len(r.Foreign) != 0 {
				continue
			}

			local, foreign := txtNameToOne(k)
			if len(r.Local) == 0 {
				r.Local = local
			}
			if len(r.Foreign) == 0 {
				r.Foreign = foreign
			}

			table.Relationships[k.Name] = r
		}

	}

	for _, t := range tables {
		if !t.IsJoinTable {
			continue
		}

		table := a.Tables[t.Name]

		lhs := t.FKeys[0]
		rhs := t.FKeys[1]

		lhsAlias, lhsOK := table.Relationships[lhs.Name]
		rhsAlias, rhsOK := table.Relationships[rhs.Name]

		if lhsOK && len(lhsAlias.Local) != 0 && len(lhsAlias.Foreign) != 0 &&
			rhsOK && len(rhsAlias.Local) != 0 && len(rhsAlias.Foreign) != 0 {
			continue
		}

		// Here we actually reverse the meaning of local/foreign to be
		// consistent with the way normal one-to-many relationships are done.
		// That's to say local = the side with the foreign key. Now in a many-to-many
		// if we were able to not have a join table our foreign key say "videos_id"
		// would be on the tags table. Hence the relationships should look like:
		// videos_tags.relationships.fk_video_id.local   = "Tags"
		// videos_tags.relationships.fk_video_id.foreign = "Videos"
		// Consistent, yes. Confusing? Also yes.

		lhsName, rhsName := txtNameToMany(lhs, rhs)

		if len(lhsAlias.Local) != 0 {
			rhsName = lhsAlias.Local
		} else if len(rhsAlias.Local) != 0 {
			lhsName = rhsAlias.Local
		}

		if len(lhsAlias.Foreign) != 0 {
			lhsName = lhsAlias.Foreign
		} else if len(rhsAlias.Foreign) != 0 {
			rhsName = rhsAlias.Foreign
		}

		if len(lhsAlias.Local) == 0 {
			lhsAlias.Local = rhsName
		}
		if len(lhsAlias.Foreign) == 0 {
			lhsAlias.Foreign = lhsName
		}
		if len(rhsAlias.Local) == 0 {
			rhsAlias.Local = lhsName
		}
		if len(rhsAlias.Foreign) == 0 {
			rhsAlias.Foreign = rhsName
		}

		table.Relationships[lhs.Name] = lhsAlias
		table.Relationships[rhs.Name] = rhsAlias
	}
}

// Table gets a table alias, panics if not found.
func (a Aliases) Table(table string) TableAlias {
	t, ok := a.Tables[table]
	if !ok {
		panic("could not find table aliases for: " + table)
	}

	return t
}

// Column get's a column's aliased name, panics if not found.
func (t TableAlias) Column(column string) string {
	c, ok := t.Columns[column]
	if !ok {
		panic(fmt.Sprintf("could not find column alias for: %s.%s", t.UpSingular, column))
	}

	return c
}

// Relationship looks up a relationship, panics if not found.
func (t TableAlias) Relationship(fkey string) RelationshipAlias {
	r, ok := t.Relationships[fkey]
	if !ok {
		panic(fmt.Sprintf("could not find relationship alias for: %s.%s", t.UpSingular, fkey))
	}

	return r
}

// ManyRelationship looks up a relationship alias, panics if not found.
// It will first try to look up a join table relationship, then it will
// try a normal one-to-many relationship. That's to say joinTable/joinTableFKey
// are used if they're not empty.
//
// This allows us to skip additional conditionals in the templates.
func (a Aliases) ManyRelationship(table, fkey, joinTable, joinTableFKey string) RelationshipAlias {
	var lookupTable, lookupFKey string
	if len(joinTable) != 0 {
		lookupTable, lookupFKey = joinTable, joinTableFKey
	} else {
		lookupTable, lookupFKey = table, fkey
	}

	t := a.Table(lookupTable)
	return t.Relationship(lookupFKey)
}
<|endoftext|>


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

package version

var (
	// Major is the current major version of master branch.
	Major = 0
	// Minor is the current minor version of master branch.
	Minor = 3
	// Patch is the curernt patched version of the master branch.
	Patch = 0
	// Release is the current release level of the master branch. Valid values
	// are dev (developement unreleased), rcX (release candidate with current
	// iteration), stable (indicates a final released version).
	Release = "stable"
)
<commit_msg>Bump minor version after stable release<commit_after>// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

var (
	// Major is the current major version of master branch.
	Major = 0
	// Minor is the current minor version of master branch.
	Minor = 3
	// Patch is the curernt patched version of the master branch.
	Patch = 1
	// Release is the current release level of the master branch. Valid values
	// are dev (developement unreleased), rcX (release candidate with current
	// iteration), stable (indicates a final released version).
	Release = "dev"
)
<|endoftext|>