gc_test.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/ruby
  2. #
  3. # generated_code.rb is in the same directory as this test.
  4. $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
  5. old_gc = GC.stress
  6. # Ruby 2.7.0 - 2.7.1 has a GC bug in its parser, so turn off stress for now
  7. # See https://bugs.ruby-lang.org/issues/16807
  8. GC.stress = 0x01 | 0x04 unless RUBY_VERSION.match?(/^2\.7\./)
  9. require 'generated_code_pb'
  10. require 'generated_code_proto2_pb'
  11. GC.stress = old_gc
  12. require 'test/unit'
  13. class GCTest < Test::Unit::TestCase
  14. def get_msg_proto3
  15. A::B::C::TestMessage.new(
  16. :optional_int32 => 1,
  17. :optional_int64 => 1,
  18. :optional_uint32 => 1,
  19. :optional_uint64 => 1,
  20. :optional_bool => true,
  21. :optional_double => 1.0,
  22. :optional_float => 1.0,
  23. :optional_string => "a",
  24. :optional_bytes => "b",
  25. :optional_enum => A::B::C::TestEnum::A,
  26. :optional_msg => A::B::C::TestMessage.new(),
  27. :repeated_int32 => [1],
  28. :repeated_int64 => [1],
  29. :repeated_uint32 => [1],
  30. :repeated_uint64 => [1],
  31. :repeated_bool => [true],
  32. :repeated_double => [1.0],
  33. :repeated_float => [1.0],
  34. :repeated_string => ["a"],
  35. :repeated_bytes => ["b"],
  36. :repeated_enum => [A::B::C::TestEnum::A],
  37. :repeated_msg => [A::B::C::TestMessage.new()],
  38. :map_int32_string => {1 => "a"},
  39. :map_int64_string => {1 => "a"},
  40. :map_uint32_string => {1 => "a"},
  41. :map_uint64_string => {1 => "a"},
  42. :map_bool_string => {true => "a"},
  43. :map_string_string => {"a" => "a"},
  44. :map_string_msg => {"a" => A::B::C::TestMessage.new()},
  45. :map_string_int32 => {"a" => 1},
  46. :map_string_bool => {"a" => true},
  47. )
  48. end
  49. def get_msg_proto2
  50. A::B::Proto2::TestMessage.new(
  51. :optional_int32 => 1,
  52. :optional_int64 => 1,
  53. :optional_uint32 => 1,
  54. :optional_uint64 => 1,
  55. :optional_bool => true,
  56. :optional_double => 1.0,
  57. :optional_float => 1.0,
  58. :optional_string => "a",
  59. :optional_bytes => "b",
  60. :optional_enum => A::B::Proto2::TestEnum::A,
  61. :optional_msg => A::B::Proto2::TestMessage.new(),
  62. :repeated_int32 => [1],
  63. :repeated_int64 => [1],
  64. :repeated_uint32 => [1],
  65. :repeated_uint64 => [1],
  66. :repeated_bool => [true],
  67. :repeated_double => [1.0],
  68. :repeated_float => [1.0],
  69. :repeated_string => ["a"],
  70. :repeated_bytes => ["b"],
  71. :repeated_enum => [A::B::Proto2::TestEnum::A],
  72. :repeated_msg => [A::B::Proto2::TestMessage.new()],
  73. :required_int32 => 1,
  74. :required_int64 => 1,
  75. :required_uint32 => 1,
  76. :required_uint64 => 1,
  77. :required_bool => true,
  78. :required_double => 1.0,
  79. :required_float => 1.0,
  80. :required_string => "a",
  81. :required_bytes => "b",
  82. :required_enum => A::B::Proto2::TestEnum::A,
  83. :required_msg => A::B::Proto2::TestMessage.new(),
  84. )
  85. end
  86. def test_generated_msg
  87. old_gc = GC.stress
  88. GC.stress = 0x01 | 0x04
  89. from = get_msg_proto3
  90. data = A::B::C::TestMessage.encode(from)
  91. to = A::B::C::TestMessage.decode(data)
  92. # This doesn't work for proto2 on JRuby because there is a nested required message.
  93. # A::B::Proto2::TestMessage has :required_msg which is of type:
  94. # A::B::Proto2::TestMessage so there is no way to generate a valid
  95. # message that doesn't exceed the depth limit
  96. if !defined? JRUBY_VERSION
  97. from = get_msg_proto2
  98. data = A::B::Proto2::TestMessage.encode(from)
  99. to = A::B::Proto2::TestMessage.decode(data)
  100. end
  101. GC.stress = old_gc
  102. puts "passed"
  103. end
  104. end