Skip to content

Commit

Permalink
added more structure
Browse files Browse the repository at this point in the history
  • Loading branch information
arnecls committed Jan 6, 2016
1 parent 44f1238 commit c9e5d22
Show file tree
Hide file tree
Showing 24 changed files with 302 additions and 115 deletions.
7 changes: 4 additions & 3 deletions metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package tgo

import (
"github.com/trivago/tgo/ttesting"
"sync"
"testing"
)
Expand All @@ -27,7 +28,7 @@ func getMockMetric() metrics {
}

func TestMetricsSet(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
mockMetric := getMockMetric()

// test for initialization to zero
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestMetricsSet(t *testing.T) {
}

func TestMetricsAddSub(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
mockMetric := getMockMetric()

mockMetric.New("MockMetric")
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestMetricsAddSub(t *testing.T) {
}

func TestMetricsIncDec(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
mockMetric := getMockMetric()
mockMetric.New("MockMetric")

Expand Down
47 changes: 47 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2015 trivago GmbH
//
// 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 tgo

import (
"github.com/trivago/tgo/treflect"
"log"
"os"
"runtime/debug"
)

// TypeRegistry is the global typeRegistry instance.
// Use this instance to register plugins.
var TypeRegistry = treflect.NewTypeRegistry()

// RecoverShutdown will trigger a shutdown via interrupt if a panic was issued.
// Typically used as "defer RecoverShutdown()".
func RecoverShutdown() {
if r := recover(); r != nil {
log.Print("Panic triggered shutdown: ", r)
log.Print(string(debug.Stack()))

// Send interrupt = clean shutdown
// TODO: Hack
proc, _ := os.FindProcess(os.Getpid())
proc.Signal(os.Interrupt)
}
}

// DontPanic can be used instead of RecoverShutdown when using a function
// without any parameters. E.g. go DontPanic(myFunction)
func DontPanic(callback func()) {
defer RecoverShutdown()
callback()
}
2 changes: 1 addition & 1 deletion marshalmap.go → tcontainer/marshalmap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tgo
package tcontainer

import (
"fmt"
Expand Down
17 changes: 9 additions & 8 deletions marshalmap_test.go → tcontainer/marshalmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tgo
package tcontainer

import (
"github.com/trivago/tgo/ttesting"
"testing"
)

func TestMarshalMapBaseTypes(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

testMap["t1"] = 10
Expand All @@ -44,7 +45,7 @@ func TestMarshalMapBaseTypes(t *testing.T) {
}

func TestMarshalMapArrays(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

iArray := []interface{}{"test", 0}
Expand All @@ -61,7 +62,7 @@ func TestMarshalMapArrays(t *testing.T) {
}

func TestMarshalMapMaps(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

iiMap := make(map[interface{}]interface{})
Expand All @@ -74,7 +75,7 @@ func TestMarshalMapMaps(t *testing.T) {
}

func TestMarshalMapMarshalMaps(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

siMap := NewMarshalMap()
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestMarshalMapMarshalMaps(t *testing.T) {
}

func TestMarshalMapStringMaps(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

ssMap := make(map[string]string)
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestMarshalMapStringMaps(t *testing.T) {
}

func TestMarshalMapStringArrayMaps(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

// String array map
Expand Down Expand Up @@ -174,7 +175,7 @@ func TestMarshalMapStringArrayMaps(t *testing.T) {
}

func TestMarshalMapPath(t *testing.T) {
expect := NewExpect(t)
expect := ttesting.NewExpect(t)
testMap := NewMarshalMap()

nestedMap1 := make(map[string]interface{})
Expand Down
2 changes: 1 addition & 1 deletion trie.go → tcontainer/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tgo
package tcontainer

// TrieNode represents a single node inside a trie.
// Each node can contain a payload which can be retrieved after a successfull
Expand Down
4 changes: 2 additions & 2 deletions tio/bufferedreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package tio
import (
"bytes"
"encoding/binary"
"github.com/trivago/tgo"
"github.com/trivago/tgo/tstrings"
"io"
)

Expand Down Expand Up @@ -198,7 +198,7 @@ func (buffer *BufferedReader) parseDelimiter() ([]byte, int) {
// messages are separeated length encoded by ASCII number and (an optional)
// delimiter.
func (buffer *BufferedReader) parseMLEText() ([]byte, int) {
messageLen, msgStartIdx := tgo.Btoi(buffer.data[buffer.paramMLE:buffer.end])
messageLen, msgStartIdx := tstrings.Btoi(buffer.data[buffer.paramMLE:buffer.end])
if msgStartIdx == 0 {
return nil, -1 // ### return, malformed ###
}
Expand Down
24 changes: 12 additions & 12 deletions tio/bufferedreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ package tio
import (
"bytes"
"fmt"
"github.com/trivago/tgo"
"github.com/trivago/tgo/ttesting"
"io"
"strings"
"testing"
)

type bufferedReaderTestData struct {
expect tgo.Expect
expect ttesting.Expect
tokens []string
parsed int
}
Expand All @@ -36,7 +36,7 @@ func (br *bufferedReaderTestData) write(data []byte, seq uint64) {

func TestBufferedReaderDelimiter(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -56,7 +56,7 @@ func TestBufferedReaderDelimiter(t *testing.T) {

func TestBufferedReaderMLEText(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -80,7 +80,7 @@ func TestBufferedReaderMLEText(t *testing.T) {

func TestBufferedReaderFixed(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test2", "test3"},
parsed: 0,
}
Expand All @@ -104,7 +104,7 @@ func TestBufferedReaderFixed(t *testing.T) {

func TestBufferedReaderMLE8(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -129,7 +129,7 @@ func TestBufferedReaderMLE8(t *testing.T) {

func TestBufferedReaderMLE16(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -154,7 +154,7 @@ func TestBufferedReaderMLE16(t *testing.T) {

func TestBufferedReaderMLE32(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -179,7 +179,7 @@ func TestBufferedReaderMLE32(t *testing.T) {

func TestBufferedReaderMLE64(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -204,7 +204,7 @@ func TestBufferedReaderMLE64(t *testing.T) {

func TestBufferedReaderMLE8EO(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -230,7 +230,7 @@ func TestBufferedReaderMLE8EO(t *testing.T) {

func TestBufferedReaderMLETextEO(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand All @@ -252,7 +252,7 @@ func TestBufferedReaderMLETextEO(t *testing.T) {

func TestBufferedReaderDelimiterE(t *testing.T) {
data := bufferedReaderTestData{
expect: tgo.NewExpect(t),
expect: ttesting.NewExpect(t),
tokens: []string{"test1", "test 2", "test\t3"},
parsed: 0,
}
Expand Down
6 changes: 3 additions & 3 deletions tio/bytestream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package tio

import (
"github.com/trivago/tgo"
"github.com/trivago/tgo/ttesting"
"testing"
)

func TestByteWriteByte(t *testing.T) {
expect := tgo.NewExpect(t)
expect := ttesting.NewExpect(t)
stream := NewByteStream(1)

data := []byte("a")
Expand All @@ -29,7 +29,7 @@ func TestByteWriteByte(t *testing.T) {
}

func TestByteStream(t *testing.T) {
expect := tgo.NewExpect(t)
expect := ttesting.NewExpect(t)

stream := NewByteStream(1)
expect.Equal(1, stream.Cap())
Expand Down
8 changes: 4 additions & 4 deletions tio/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
package tio

import (
"github.com/trivago/tgo"
"github.com/trivago/tgo/ttesting"
"os"
"sort"
"testing"
"time"
)

func TestListFilesByDateMatching(t *testing.T) {
expect := tgo.NewExpect(t)
expect := ttesting.NewExpect(t)

files, err := ListFilesByDateMatching(".", "\\.go$")
expect.NoError(err)
Expand Down Expand Up @@ -75,7 +75,7 @@ func (info fileInfoMock) Sys() interface{} {
}

func TestSplitPath(t *testing.T) {
expect := tgo.NewExpect(t)
expect := ttesting.NewExpect(t)

dir, name, ext := SplitPath("a/b")

Expand All @@ -97,7 +97,7 @@ func TestSplitPath(t *testing.T) {
}

func TestFilesByDate(t *testing.T) {
expect := tgo.NewExpect(t)
expect := ttesting.NewExpect(t)

testData := FilesByDate{
fileInfoMock{name: "log1", mod: time.Unix(1, 0)},
Expand Down
Loading

0 comments on commit c9e5d22

Please sign in to comment.