本文整理匯總了Golang中github.com/rafrombrc/gospec/src/gospec.Context.Assume方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Assume方法的具體用法?Golang Context.Assume怎麽用?Golang Context.Assume使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/rafrombrc/gospec/src/gospec.Context
的用法示例。
在下文中一共展示了Context.Assume方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: DecodersSpec
func DecodersSpec(c gospec.Context) {
msg := getTestMessage()
c.Specify("A JsonDecoder", func() {
var fmtString = `{"type":"%s","timestamp":%s,"logger":"%s","severity":%d,"payload":"%s","fields":%s,"env_version":"%s","metlog_pid":%d,"metlog_hostname":"%s"}`
timestampJson, err := json.Marshal(msg.Timestamp)
fieldsJson, err := json.Marshal(msg.Fields)
c.Assume(err, gs.IsNil)
jsonString := fmt.Sprintf(fmtString, msg.Type,
timestampJson, msg.Logger, msg.Severity, msg.Payload,
fieldsJson, msg.Env_version, msg.Pid, msg.Hostname)
pipelinePack := getTestPipelinePack()
pipelinePack.MsgBytes = []byte(jsonString)
jsonDecoder := new(JsonDecoder)
c.Specify("can decode a JSON message", func() {
err := jsonDecoder.Decode(pipelinePack)
c.Expect(pipelinePack.Message, gs.Equals, msg)
c.Expect(err, gs.IsNil)
})
c.Specify("returns `fields` as a map", func() {
jsonDecoder.Decode(pipelinePack)
c.Expect(pipelinePack.Message.Fields["foo"], gs.Equals, "bar")
})
c.Specify("returns an error for bogus JSON", func() {
badJson := fmt.Sprint("{{", jsonString)
pipelinePack.MsgBytes = []byte(badJson)
err := jsonDecoder.Decode(pipelinePack)
c.Expect(err, gs.Not(gs.IsNil))
c.Expect(pipelinePack.Message.Timestamp.IsZero(), gs.IsTrue)
})
})
c.Specify("A MsgPackDecoder", func() {
msg := getTestMessage()
encoded, err := msgpack.Marshal(msg)
c.Assume(err, gs.IsNil)
decoder := new(MsgPackDecoder)
decoder.Init(nil)
pack := getTestPipelinePack()
c.Specify("decodes a msgpack message", func() {
pack.MsgBytes = encoded
err := decoder.Decode(pack)
c.Expect(err, gs.IsNil)
c.Expect(pack.Message, gs.Equals, msg)
})
c.Specify("returns an error for bunk encoding", func() {
bunk := append([]byte{0, 0, 0}, encoded...)
pack.MsgBytes = bunk
err := decoder.Decode(pack)
c.Expect(err, gs.Not(gs.IsNil))
})
})
}
示例2: CheckTypeSpec
func CheckTypeSpec(c gs.Context) {
var err error
var tomlBlob = `
ranking = ["Springsteen", "J Geils"]
[bands.Springsteen]
type = "ignore_this"
started = 1973
albums = ["Greetings", "WIESS", "Born to Run", "Darkness"]
not_albums = ["Greetings", "WIESS", "Born to Run", "Darkness"]
[bands.J Geils]
started = 1970
albums = ["The J. Geils Band", "Full House", "Blow Your Face Out"]
`
type classics struct {
Ranking []string
Bands map[string]Primitive
}
c.Specify("check mapping", func() {
// Do the initial decode. Reflection is delayed on Primitive values.
var music classics
var md MetaData
md, err = Decode(tomlBlob, &music)
c.Assume(err, gs.IsNil)
empty_ignore := map[string]interface{}{}
err = CheckType(md.mapping, music, empty_ignore)
c.Assume(err, gs.IsNil)
})
}
示例3: StatsdInputSpec
func StatsdInputSpec(c gs.Context) {
t := &pipeline_ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := NewPipelineConfig(nil)
ith := new(plugins_ts.InputTestHelper)
ith.Msg = pipeline_ts.GetTestMessage()
ith.Pack = NewPipelinePack(pConfig.InputRecycleChan())
ith.PackSupply = make(chan *PipelinePack, 1)
// Specify localhost, but we're not really going to use the network
ith.AddrStr = "localhost:55565"
ith.ResolvedAddrStr = "127.0.0.1:55565"
// set up mock helper, input runner, and stat accumulator
ith.MockHelper = NewMockPluginHelper(ctrl)
ith.MockInputRunner = NewMockInputRunner(ctrl)
mockStatAccum := NewMockStatAccumulator(ctrl)
c.Specify("A StatsdInput", func() {
statsdInput := StatsdInput{}
config := statsdInput.ConfigStruct().(*StatsdInputConfig)
config.Address = ith.AddrStr
err := statsdInput.Init(config)
c.Assume(err, gs.IsNil)
realListener := statsdInput.listener
c.Expect(realListener.LocalAddr().String(), gs.Equals, ith.ResolvedAddrStr)
realListener.Close()
mockListener := pipeline_ts.NewMockConn(ctrl)
statsdInput.listener = mockListener
ith.MockHelper.EXPECT().StatAccumulator("StatAccumInput").Return(mockStatAccum, nil)
mockListener.EXPECT().Close()
mockListener.EXPECT().SetReadDeadline(gomock.Any())
c.Specify("sends a Stat to the StatAccumulator", func() {
statName := "sample.count"
statVal := 303
msg := fmt.Sprintf("%s:%d|c\n", statName, statVal)
expected := Stat{statName, strconv.Itoa(statVal), "c", float32(1)}
mockStatAccum.EXPECT().DropStat(expected).Return(true)
readCall := mockListener.EXPECT().Read(make([]byte, 512))
readCall.Return(len(msg), nil)
readCall.Do(func(msgBytes []byte) {
copy(msgBytes, []byte(msg))
statsdInput.Stop()
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
err = statsdInput.Run(ith.MockInputRunner, ith.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
wg.Wait()
})
})
}
示例4: InsensitiveDecodeSpec
func InsensitiveDecodeSpec(c gs.Context) {
tme, err := time.Parse(time.RFC3339, time.RFC3339[:len(time.RFC3339)-5])
if err != nil {
panic(err)
}
expected := Insensitive{
TopString: "string",
TopInt: 1,
TopFloat: 1.1,
TopBool: true,
TopDate: tme,
TopArray: []string{"array"},
MatcH: "i should be in MatcH only",
Match: "i should be in Match only",
Field: "neat", // encoding/json would store "messy" here
Once: "just once",
OncE: "just once", // wait, what?
Nest: InsensitiveNest{
Ed: InsensitiveEd{NestedString: "another string"},
},
}
var got Insensitive
_, err = Decode(caseToml, &got)
c.Assume(err, gs.IsNil)
c.Assume(reflect.DeepEqual(expected, got), gs.IsTrue)
}
示例5: ProtobufDecoderSpec
func ProtobufDecoderSpec(c gospec.Context) {
t := &ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
msg := ts.GetTestMessage()
config := NewPipelineConfig(nil) // Initializes globals.
c.Specify("A ProtobufDecoder", func() {
encoded, err := proto.Marshal(msg)
c.Assume(err, gs.IsNil)
pack := NewPipelinePack(config.inputRecycleChan)
decoder := new(ProtobufDecoder)
decoder.sampleDenominator = 1000 // Since we don't call decoder.Init().
c.Specify("decodes a protobuf message", func() {
pack.MsgBytes = encoded
_, err := decoder.Decode(pack)
c.Expect(err, gs.IsNil)
c.Expect(pack.Message, gs.Equals, msg)
v, ok := pack.Message.GetFieldValue("foo")
c.Expect(ok, gs.IsTrue)
c.Expect(v, gs.Equals, "bar")
})
c.Specify("returns an error for bunk encoding", func() {
bunk := append([]byte{0, 0, 0}, encoded...)
pack.MsgBytes = bunk
_, err := decoder.Decode(pack)
c.Expect(err, gs.Not(gs.IsNil))
})
})
}
示例6: UdpInputSpecFailure
func UdpInputSpecFailure(c gs.Context) {
udpInput := UdpInput{}
err := udpInput.Init(&UdpInputConfig{Net: "tcp", Address: "localhost:55565",
Decoder: "ProtobufDecoder",
ParserType: "message.proto"})
c.Assume(err, gs.Not(gs.IsNil))
c.Assume(err.Error(), gs.Equals, "ResolveUDPAddr failed: unknown network tcp\n")
}
示例7: MessageTemplateSpec
func MessageTemplateSpec(c gs.Context) {
c.Specify("A message template", func() {
mt := make(MessageTemplate)
mt["Logger"] = "test logger"
mt["Payload"] = "test payload"
mt["Hostname"] = "host.example.com"
mt["Pid"] = "123.456"
mt["Type"] = "test type"
mt["Severity"] = "23"
mt["tmplTest|baz"] = "bar"
msg := ts.GetTestMessage()
c.Specify("replaces message values", func() {
err := mt.PopulateMessage(msg, nil)
c.Assume(err, gs.IsNil)
c.Expect(msg.GetLogger(), gs.Equals, mt["Logger"])
c.Expect(msg.GetPayload(), gs.Equals, mt["Payload"])
c.Expect(msg.GetHostname(), gs.Equals, mt["Hostname"])
c.Expect(msg.GetPid(), gs.Equals, int32(123))
c.Expect(msg.GetType(), gs.Equals, mt["Type"])
c.Expect(msg.GetSeverity(), gs.Equals, int32(23))
fields := msg.FindAllFields("tmplTest")
c.Expect(len(fields), gs.Equals, 1)
field := fields[0]
value := field.GetValueString()
c.Expect(len(value), gs.Equals, 1)
c.Expect(value[0], gs.Equals, "bar")
c.Expect(field.GetRepresentation(), gs.Equals, "baz")
})
c.Specify("honors substitutions", func() {
mt["Payload"] = "this is %substitution%"
mt["Hostname"] = "%host%.example.com"
mt["Type"] = "another %substitution%"
mt["tmplTest|baz"] = "%fieldvalue%"
subs := make(map[string]string)
subs["host"] = "otherhost"
subs["substitution"] = "a test"
subs["fieldvalue"] = "wakajawaka"
err := mt.PopulateMessage(msg, subs)
c.Assume(err, gs.IsNil)
c.Expect(msg.GetLogger(), gs.Equals, mt["Logger"])
c.Expect(msg.GetPayload(), gs.Equals, "this is a test")
c.Expect(msg.GetHostname(), gs.Equals, "otherhost.example.com")
c.Expect(msg.GetPid(), gs.Equals, int32(123))
c.Expect(msg.GetType(), gs.Equals, "another a test")
c.Expect(msg.GetSeverity(), gs.Equals, int32(23))
fields := msg.FindAllFields("tmplTest")
c.Expect(len(fields), gs.Equals, 1)
field := fields[0]
value := field.GetValueString()
c.Expect(len(value), gs.Equals, 1)
c.Expect(value[0], gs.Equals, subs["fieldvalue"])
c.Expect(field.GetRepresentation(), gs.Equals, "baz")
})
})
}
示例8: PrimitiveDecodeStrictSpec
func PrimitiveDecodeStrictSpec(c gs.Context) {
var md MetaData
var err error
var tomlBlob = `
ranking = ["Springsteen", "J Geils"]
[bands.Springsteen]
type = "ignore_this"
started = 1973
albums = ["Greetings", "WIESS", "Born to Run", "Darkness"]
not_albums = ["Greetings", "WIESS", "Born to Run", "Darkness"]
[bands.J Geils]
started = 1970
albums = ["The J. Geils Band", "Full House", "Blow Your Face Out"]
`
type band struct {
Started int
Albums []string
}
type classics struct {
Ranking []string
Bands map[string]Primitive
}
// Do the initial decode. Reflection is delayed on Primitive values.
var music classics
md, err = Decode(tomlBlob, &music)
c.Assume(err, gs.IsNil)
// MetaData still includes information on Primitive values.
c.Assume(md.IsDefined("bands", "Springsteen"), gs.IsTrue)
ignore_type := map[string]interface{}{"type": true}
// Decode primitive data into Go values.
for _, artist := range music.Ranking {
// A band is a primitive value, so we need to decode it to get a
// real `band` value.
primValue := music.Bands[artist]
var aBand band
err = PrimitiveDecodeStrict(primValue, &aBand, ignore_type)
if artist == "Springsteen" {
c.Assume(err, gs.Not(gs.IsNil))
c.Expect(err.Error(), gs.Equals, "Configuration contains key [not_albums] which doesn't exist in struct")
c.Assume(1973, gs.Equals, aBand.Started)
} else {
c.Expect(err, gs.IsNil)
c.Assume(1970, gs.Equals, aBand.Started)
}
}
}
示例9: TcpInputSpecFailure
func TcpInputSpecFailure(c gs.Context) {
tcpInput := TcpInput{}
err := tcpInput.Init(&TcpInputConfig{Net: "udp", Address: "localhost:55565",
Decoder: "ProtobufDecoder",
ParserType: "message.proto"})
c.Assume(err, gs.Not(gs.IsNil))
c.Assume(err.Error(), gs.Equals, "ListenTCP failed: unknown network udp\n")
}
示例10: WhisperRunnerSpec
func WhisperRunnerSpec(c gospec.Context) {
tmpDir := os.TempDir()
tmpFileName := fmt.Sprintf("heka-%d.wsp", time.Now().UTC().UnixNano())
tmpFileName = filepath.Join(tmpDir, tmpFileName)
interval := uint32(10)
archiveInfo := []whisper.ArchiveInfo{
whisper.ArchiveInfo{0, interval, 60},
whisper.ArchiveInfo{0, 60, 8},
}
c.Specify("A WhisperRunner", func() {
var wg sync.WaitGroup
wg.Add(1)
folderPerm := os.FileMode(0755)
wr, err := NewWhisperRunner(tmpFileName, archiveInfo, whisper.AggregationSum,
folderPerm, &wg)
c.Assume(err, gs.IsNil)
defer func() {
os.Remove(tmpFileName)
}()
c.Specify("creates a whisper file of the correct size", func() {
fi, err := os.Stat(tmpFileName)
c.Expect(err, gs.IsNil)
c.Expect(fi.Size(), gs.Equals, int64(856))
close(wr.InChan())
wg.Wait()
})
c.Specify("writes a data point to the whisper file", func() {
// Send a data point through and close.
when := time.Now().UTC()
val := float64(6)
pt := whisper.NewPoint(when, val)
wr.InChan() <- &pt
close(wr.InChan())
wg.Wait()
// Open db file and fetch interval including our data point.
from := when.Add(-1 * time.Second).Unix()
until := when.Add(1 * time.Second).Unix()
db, err := whisper.Open(tmpFileName)
c.Expect(err, gs.IsNil)
_, fetched, _ := db.FetchUntil(uint32(from), uint32(until))
// Verify that our value is stored in the most recent interval and
// that the diff btn our timestamp and the stored time value is
// less than the interval duration.
fpt := fetched[len(fetched)-1]
diff := when.Sub(fpt.Time().UTC())
c.Expect(diff.Seconds() < float64(interval), gs.IsTrue)
c.Expect(fpt.Value, gs.Equals, val)
})
})
}
示例11: UdpInputSpecFailure
func UdpInputSpecFailure(c gs.Context) {
udpInput := UdpInput{}
err := udpInput.Init(&UdpInputConfig{
Net: "tcp",
Address: "localhost:55565",
})
c.Assume(err, gs.Not(gs.IsNil))
c.Assume(err.Error(), gs.Equals, "ResolveUDPAddr failed: unknown network tcp\n")
}
示例12: TcpInputSpecFailure
func TcpInputSpecFailure(c gs.Context) {
tcpInput := TcpInput{}
err := tcpInput.Init(&TcpInputConfig{
Net: "udp",
Address: "localhost:55565",
})
c.Assume(err, gs.Not(gs.IsNil))
c.Assume(err.Error(), gs.Equals, "ResolveTCPAddress failed: unknown network udp\n")
}
示例13: DecodeSpec
func DecodeSpec(c gs.Context) {
var val simple
md, err := Decode(testSimple, &val)
c.Assume(err, gs.IsNil)
c.Assume(md.IsDefined("Annoying", "Cats", "plato"), gs.IsTrue)
c.Assume(md.IsDefined("Cats", "Stinky"), gs.IsFalse)
var colors = [][]string{[]string{"red", "green", "blue"},
[]string{"cyan", "magenta", "yellow", "black"}}
for ridx, row := range colors {
for cidx, _ := range row {
c.Assume(val.Colors[ridx][cidx], gs.Equals, colors[ridx][cidx])
}
}
c.Assume(val, gs.Not(gs.IsNil))
}
示例14: HttpListenInputSpec
func HttpListenInputSpec(c gs.Context) {
t := &pipeline_ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := NewPipelineConfig(nil)
httpListenInput := HttpListenInput{}
ith := new(plugins_ts.InputTestHelper)
ith.MockHelper = pipelinemock.NewMockPluginHelper(ctrl)
ith.MockInputRunner = pipelinemock.NewMockInputRunner(ctrl)
startInput := func() {
go func() {
httpListenInput.Run(ith.MockInputRunner, ith.MockHelper)
}()
}
ith.Pack = NewPipelinePack(pConfig.InputRecycleChan())
ith.PackSupply = make(chan *PipelinePack, 1)
config := httpListenInput.ConfigStruct().(*HttpListenInputConfig)
config.Address = "127.0.0.1:8325"
config.Decoder = "PayloadJsonDecoder"
ith.MockHelper.EXPECT().PipelineConfig().Return(pConfig)
mockDecoderRunner := pipelinemock.NewMockDecoderRunner(ctrl)
dRunnerInChan := make(chan *PipelinePack, 1)
mockDecoderRunner.EXPECT().InChan().Return(dRunnerInChan).AnyTimes()
ith.MockInputRunner.EXPECT().InChan().Return(ith.PackSupply).AnyTimes()
ith.MockInputRunner.EXPECT().Name().Return("HttpListenInput").AnyTimes()
ith.MockHelper.EXPECT().DecoderRunner("PayloadJsonDecoder", "HttpListenInput-PayloadJsonDecoder").Return(mockDecoderRunner, true)
err := httpListenInput.Init(config)
c.Assume(err, gs.IsNil)
ith.MockInputRunner.EXPECT().LogMessage(gomock.Any())
startInput()
c.Specify("A HttpListenInput", func() {
c.Specify("Adds query parameters to the message pack as fields", func() {
ith.PackSupply <- ith.Pack
resp, err := http.Get("http://127.0.0.1:8325/?test=Hello%20World")
c.Assume(err, gs.IsNil)
resp.Body.Close()
c.Assume(resp.StatusCode, gs.Equals, 200)
pack := <-dRunnerInChan
fieldValue, ok := pack.Message.GetFieldValue("test")
c.Assume(ok, gs.IsTrue)
c.Expect(fieldValue, gs.Equals, "Hello World")
})
httpListenInput.Stop()
})
}
示例15: DecodeStrictInterfaceSpec
func DecodeStrictInterfaceSpec(c gs.Context) {
// Check that we can safely decode into an empty interface
// properly
var tomlBlob = `
[MyMultiDecoder]
type = "MultiDecoder"
order = ["MyJsonDecoder", "MyProtobufDecoder"]
[MyMultiDecoder.delegates.MyJsonDecoder]
type = "JsonDecoder"
encoding_name = "JSON"
[MyMultiDecoder.delegates.MyProtobufDecoder]
type = "ProtobufDecoder"
encoding_name = "PROTOCOL_BUFFER"
`
var err error
var obj interface{}
empty_ignore := map[string]interface{}{}
_, err = DecodeStrict(tomlBlob, &obj, empty_ignore)
c.Assume(err, gs.IsNil)
actualObj := obj.(map[string]interface{})
multidecoder := actualObj["MyMultiDecoder"].(map[string]interface{})
c.Expect(multidecoder["type"], gs.Equals, "MultiDecoder")
order := multidecoder["order"].([]interface{})
d1 := order[0].(string)
d2 := order[1].(string)
c.Expect(d1, gs.Equals, "MyJsonDecoder")
c.Expect(d2, gs.Equals, "MyProtobufDecoder")
delegates := multidecoder["delegates"].(map[string]interface{})
myjson := delegates["MyJsonDecoder"].(map[string]interface{})
myproto := delegates["MyProtobufDecoder"].(map[string]interface{})
c.Expect(myjson["type"], gs.Equals, "JsonDecoder")
c.Expect(myjson["encoding_name"], gs.Equals, "JSON")
c.Expect(myproto["type"], gs.Equals, "ProtobufDecoder")
c.Expect(myproto["encoding_name"], gs.Equals, "PROTOCOL_BUFFER")
}