You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

  1. package operation
  2. import (
  3. proto "code.google.com/p/goprotobuf/proto"
  4. "encoding/json"
  5. "log"
  6. "testing"
  7. )
  8. func TestSerialDeserial(t *testing.T) {
  9. volumeMessage := &VolumeInformationMessage{
  10. Id: proto.Uint32(12),
  11. Size: proto.Uint64(2341234),
  12. Collection: proto.String("benchmark"),
  13. FileCount: proto.Uint64(2341234),
  14. DeleteCount: proto.Uint64(234),
  15. DeletedByteCount: proto.Uint64(21234),
  16. ReadOnly: proto.Bool(false),
  17. ReplicaPlacement: proto.Uint32(210),
  18. Version: proto.Uint32(2),
  19. }
  20. var volumeMessages []*VolumeInformationMessage
  21. volumeMessages = append(volumeMessages, volumeMessage)
  22. joinMessage := &JoinMessage{
  23. IsInit: proto.Bool(true),
  24. Ip: proto.String("127.0.3.12"),
  25. Port: proto.Uint32(34546),
  26. PublicUrl: proto.String("localhost:2342"),
  27. MaxVolumeCount: proto.Uint32(210),
  28. MaxFileKey: proto.Uint64(324234423),
  29. DataCenter: proto.String("dc1"),
  30. Rack: proto.String("rack2"),
  31. Volumes: volumeMessages,
  32. }
  33. data, err := proto.Marshal(joinMessage)
  34. if err != nil {
  35. log.Fatal("marshaling error: ", err)
  36. }
  37. newMessage := &JoinMessage{}
  38. err = proto.Unmarshal(data, newMessage)
  39. if err != nil {
  40. log.Fatal("unmarshaling error: ", err)
  41. }
  42. log.Println("The pb data size is", len(data))
  43. jsonData, jsonError := json.Marshal(joinMessage)
  44. if jsonError != nil {
  45. log.Fatal("json marshaling error: ", jsonError)
  46. }
  47. log.Println("The json data size is", len(jsonData), string(jsonData))
  48. // Now test and newTest contain the same data.
  49. if *joinMessage.PublicUrl != *newMessage.PublicUrl {
  50. log.Fatalf("data mismatch %q != %q", *joinMessage.PublicUrl, *newMessage.PublicUrl)
  51. }
  52. }