identity_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package tracing
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestNewIdentity(t *testing.T) {
  7. testCases := []struct {
  8. testCase string
  9. trace string
  10. expected string
  11. }{
  12. {
  13. testCase: "full length trace",
  14. trace: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
  15. expected: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
  16. },
  17. {
  18. testCase: "short trace ID",
  19. trace: "ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
  20. expected: "0000ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
  21. },
  22. {
  23. testCase: "short trace ID with 0s in the middle",
  24. trace: "ad8a01fde11f000002efdce36873:52726f6cabc144f5:0:1",
  25. expected: "0000ad8a01fde11f000002efdce36873:52726f6cabc144f5:0:1",
  26. },
  27. {
  28. testCase: "short trace ID with 0s in the beginning and middle",
  29. trace: "001ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
  30. expected: "0001ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0:1",
  31. },
  32. {
  33. testCase: "no trace",
  34. trace: "",
  35. },
  36. {
  37. testCase: "missing flags",
  38. trace: "ec31ad8a01fde11fdcabe2efdce36873:52726f6cabc144f5:0",
  39. },
  40. {
  41. testCase: "missing separator",
  42. trace: "ec31ad8a01fde11fdcabe2efdce3687352726f6cabc144f501",
  43. },
  44. }
  45. for _, testCase := range testCases {
  46. identity, err := NewIdentity(testCase.trace)
  47. if testCase.expected != "" {
  48. require.NoError(t, err)
  49. require.Equal(t, testCase.expected, identity.String())
  50. serializedIdentity, err := identity.MarshalBinary()
  51. require.NoError(t, err)
  52. deserializedIdentity := new(Identity)
  53. err = deserializedIdentity.UnmarshalBinary(serializedIdentity)
  54. require.NoError(t, err)
  55. require.Equal(t, identity, deserializedIdentity)
  56. } else {
  57. require.Error(t, err)
  58. require.Nil(t, identity)
  59. }
  60. }
  61. }