forked from msgpack/msgpack-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp_spec.rb
More file actions
159 lines (129 loc) · 5.4 KB
/
timestamp_spec.rb
File metadata and controls
159 lines (129 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true
require 'spec_helper'
describe MessagePack::Timestamp do
describe 'malformed format' do
it do
expect do
MessagePack::Timestamp.from_msgpack_ext([0xd4, 0x00].pack("C*"))
end.to raise_error(MessagePack::MalformedFormatError)
end
end
describe 'register_type with Time' do
let(:factory) do
factory = MessagePack::Factory.new
factory.register_type(
MessagePack::Timestamp::TYPE,
Time,
packer: MessagePack::Time::Packer,
unpacker: MessagePack::Time::Unpacker
)
factory
end
let(:time) { Time.local(2019, 6, 17, 1, 2, 3, 123_456_789 / 1000.0) }
it 'serializes and deserializes Time' do
prefix_fixext8_with_type_id = [0xd7, -1].pack("c*")
packed = factory.pack(time)
expect(packed).to start_with(prefix_fixext8_with_type_id)
expect(packed.size).to eq(10)
unpacked = factory.unpack(packed)
expect(unpacked.to_i).to eq(time.to_i)
expect(unpacked.to_f).to eq(time.to_f)
# expect(unpacked).to eq(time) # we can't do it because of nsec (rational vs float?)
end
let(:time_without_nsec) { Time.local(2019, 6, 17, 1, 2, 3, 0) }
it 'serializes time without nanosec as fixext4' do
prefix_fixext4_with_type_id = [0xd6, -1].pack("c*")
packed = factory.pack(time_without_nsec)
expect(packed).to start_with(prefix_fixext4_with_type_id)
expect(packed.size).to eq(6)
unpacked = factory.unpack(packed)
expect(unpacked).to eq(time_without_nsec)
end
let(:time_after_2514) { Time.at(1 << 34) } # the max num of 34bit int means 2514-05-30 01:53:04 UTC
it 'serializes time after 2038 as ext8' do
prefix_ext8_with_12bytes_payload_and_type_id = [0xc7, 12, -1].pack("c*")
expect(time_after_2514.to_i).to be > 0xffffffff
packed = factory.pack(time_after_2514)
expect(packed).to start_with(prefix_ext8_with_12bytes_payload_and_type_id)
expect(packed.size).to eq(15)
end
it 'runs correctly (regression)' do
expect(factory.unpack(factory.pack(Time.utc(2200)))).to eq(Time.utc(2200))
end
let(:time32_max) { Time.new(2106, 2, 7, 6, 28, 15, "+00:00") }
it 'is serialized into timestamp32' do
expect(factory.pack(time32_max).size).to be 6
expect(factory.unpack(factory.pack(time32_max)).utc).to eq(time32_max)
end
let(:time64_min) { Time.new(2106, 2, 7, 6, 28, 16, "+00:00") }
it 'is serialized into timestamp64' do
expect(factory.pack(time64_min).size).to be 10
expect(factory.unpack(factory.pack(time64_min)).utc).to eq(time64_min)
end
let(:time64_max) { Time.at(Time.new(2514, 5, 30, 1, 53, 3, "+00:00").to_i, 999999999 / 1000.0r).utc } # TODO: use Time.at(sec, nsec, :nsec) when removing Ruby 2.4 from the list
it 'is serialized into timestamp64' do
expect(factory.pack(time64_max).size).to be 10
expect(factory.unpack(factory.pack(time64_max)).utc).to eq(time64_max)
end
let(:time96_positive_min) { Time.new(2514, 5, 30, 1, 53, 4, "+00:00") }
it 'is serialized into timestamp96' do
expect(factory.pack(time96_positive_min).size).to be 15
expect(factory.unpack(factory.pack(time96_positive_min)).utc).to eq(time96_positive_min)
end
let(:time96_min) { Time.at(-2**63).utc }
it 'is serialized into timestamp96' do
skip if IS_JRUBY || IS_TRUFFLERUBY # JRuby and TruffleRuby both use underlying Java time classes that do not support |year| >= 1 billion
expect(factory.pack(time96_min).size).to be 15
expect(factory.unpack(factory.pack(time96_min)).utc).to eq(time96_min)
end
let(:time96_max) { Time.at(2**63 - 1).utc }
it 'is serialized into timestamp96' do
skip if IS_JRUBY || IS_TRUFFLERUBY # JRuby and TruffleRuby both use underlying Java time classes that do not support |year| >= 1 billion
expect(factory.pack(time96_max).size).to be 15
expect(factory.unpack(factory.pack(time96_max)).utc).to eq(time96_max)
end
end
describe 'register_type with MessagePack::Timestamp' do
let(:factory) do
factory = MessagePack::Factory.new
factory.register_type(MessagePack::Timestamp::TYPE, MessagePack::Timestamp)
factory
end
let(:timestamp) { MessagePack::Timestamp.new(Time.now.tv_sec, 123_456_789) }
it 'serializes and deserializes MessagePack::Timestamp' do
packed = factory.pack(timestamp)
unpacked = factory.unpack(packed)
expect(unpacked).to eq(timestamp)
end
end
describe 'timestamp32' do
it 'handles [1, 0]' do
t = MessagePack::Timestamp.new(1, 0)
payload = t.to_msgpack_ext
unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
expect(unpacked).to eq(t)
end
end
describe 'timestamp64' do
it 'handles [1, 1]' do
t = MessagePack::Timestamp.new(1, 1)
payload = t.to_msgpack_ext
unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
expect(unpacked).to eq(t)
end
end
describe 'timestamp96' do
it 'handles [-1, 0]' do
t = MessagePack::Timestamp.new(-1, 0)
payload = t.to_msgpack_ext
unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
expect(unpacked).to eq(t)
end
it 'handles [-1, 999_999_999]' do
t = MessagePack::Timestamp.new(-1, 999_999_999)
payload = t.to_msgpack_ext
unpacked = MessagePack::Timestamp.from_msgpack_ext(payload)
expect(unpacked).to eq(t)
end
end
end