// Test of gowebmsgpack.MsgpackFormatter.Format(). func TestFormatterFormat(t *testing.T) { // Create and regist formatter formatter := new(MsgpackFormatter) goweb.AddFormatter(formatter) // Create context r, _ := http.NewRequest("GET", "http://localhsot:8080", nil) w := httptest.NewRecorder() pathPrams := goweb.ParameterValueMap(make(map[string]string)) cx := &goweb.Context{r, w, pathPrams, MSGPACK_FORMAT} data := []int{1, 2, 3} result := struct { C string S int D interface{} E []string }{ "", http.StatusOK, data, nil, } expect, _ := msgpack.Marshal(result) cx.RespondWithData(data) actual := w.Body.Bytes() if !bytes.Equal(expect, actual) { t.Errorf("expect %v but actual %v.", expect, actual) } }
// Test of MsgpackRequestDecoder.Decode(). func TestDecoderDecode(t *testing.T) { expect := []int{1, 2, 3} body, _ := msgpack.Marshal(expect) // Create context r, _ := http.NewRequest("GET", "http://localhsot:8080", bytes.NewBuffer(body)) w := httptest.NewRecorder() pathPrams := goweb.ParameterValueMap(make(map[string]string)) cx := &goweb.Context{r, w, pathPrams, MSGPACK_FORMAT} // Create Decoder decoder := new(MsgpackRequestDecoder) var actual []int if err := decoder.Unmarshal(cx, &actual); err != nil { t.Error(err) } if len(expect) != len(actual) { t.Errorf("expect length of array is %d but actual is %d.", len(expect), len(actual)) } for i := range expect { if expect[i] != actual[i] { t.Errorf("expect %dth element is %d but actual is %d", expect[i], actual[i]) break } } }
func Publish(input chan []*FileEvent, server_list []string, server_timeout time.Duration) { var buffer bytes.Buffer //key := "abcdefghijklmnop" //cipher, err := aes.NewCipher([]byte(key)) socket := FFS{ Endpoints: server_list, SocketType: zmq.REQ, RecvTimeout: server_timeout, SendTimeout: server_timeout, } //defer socket.Close() for events := range input { // got a bunch of events, ship them out. log.Printf("Spooler gave me %d events\n", len(events)) // Serialize with msgpack data, err := msgpack.Marshal(events) // TODO(sissel): chefk error _ = err //log.Printf("msgpack serialized %d bytes\n", len(data)) // Compress it // A new compressor is used for every payload of events so // that any individual payload can be decompressed alone. // TODO(sissel): Make compression level tunable compressor, _ := zlib.NewWriterLevel(&buffer, 3) buffer.Truncate(0) _, err = compressor.Write(data) err = compressor.Flush() compressor.Close() //log.Printf("compressed %d bytes\n", buffer.Len()) // TODO(sissel): check err // TODO(sissel): implement security/encryption/etc // Send full payload over zeromq REQ/REP // TODO(sissel): check error //buffer.Write(data) // Loop forever trying to send. // This will cause reconnects/etc on failures automatically for { err = socket.Send(buffer.Bytes(), 0) data, err = socket.Recv(0) if err == nil { // success! break } } // TODO(sissel): Check data value of reply? // TODO(sissel): retry on failure or timeout // TODO(sissel): notify registrar of success } /* for each event payload */ } // Publish
// Readies response and converts input data into Msgpack. func (f *MsgpackFormatter) Format(cx *goweb.Context, input interface{}) ([]uint8, error) { // marshal msgpack output, err := msgpack.Marshal(input) if err != nil { return nil, err } cx.ResponseWriter.Header().Set(CONTENT_TYPE, MSGPACK_CONTENT_TYPE) return output, nil }
func (t *MsgpackTest) BenchmarkStructMsgpack2(c *C) { in := t.structForBenchmark() out := &benchmarkStruct{} for i := 0; i < c.N; i++ { b, err := msgpack2.Marshal(in) if err != nil { panic(err) } err = msgpack2.Unmarshal(b, out, nil) if err != nil { panic(err) } } }
func BenchmarkStructUgorjiGoMsgpack(b *testing.B) { in := structForBenchmark() out := &benchmarkStruct{} for i := 0; i < b.N; i++ { buf, err := gomsgpack.Marshal(in) if err != nil { b.Fatal(err) } err = gomsgpack.Unmarshal(buf, out, nil) if err != nil { b.Fatal(err) } } }
// marshal into messagepack. // This function is used for websocket.Codec. func msgpackMarshal(v interface{}) (msg []byte, payloadType byte, err error) { msg, err = msgpack.Marshal(v) return msg, websocket.BinaryFrame, err }