sessionm-thrift 0.8.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +1 -0
- data/README +43 -0
- data/benchmark/Benchmark.thrift +24 -0
- data/benchmark/benchmark.rb +271 -0
- data/benchmark/client.rb +74 -0
- data/benchmark/server.rb +82 -0
- data/benchmark/thin_server.rb +44 -0
- data/ext/binary_protocol_accelerated.c +441 -0
- data/ext/binary_protocol_accelerated.h +20 -0
- data/ext/compact_protocol.c +618 -0
- data/ext/compact_protocol.h +20 -0
- data/ext/constants.h +96 -0
- data/ext/extconf.rb +30 -0
- data/ext/macros.h +41 -0
- data/ext/memory_buffer.c +131 -0
- data/ext/memory_buffer.h +20 -0
- data/ext/protocol.c +185 -0
- data/ext/protocol.h +20 -0
- data/ext/strlcpy.c +41 -0
- data/ext/strlcpy.h +30 -0
- data/ext/struct.c +691 -0
- data/ext/struct.h +25 -0
- data/ext/thrift_native.c +196 -0
- data/lib/thrift.rb +64 -0
- data/lib/thrift/client.rb +62 -0
- data/lib/thrift/core_ext.rb +23 -0
- data/lib/thrift/core_ext/fixnum.rb +29 -0
- data/lib/thrift/exceptions.rb +84 -0
- data/lib/thrift/processor.rb +57 -0
- data/lib/thrift/protocol/base_protocol.rb +290 -0
- data/lib/thrift/protocol/binary_protocol.rb +229 -0
- data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
- data/lib/thrift/protocol/compact_protocol.rb +426 -0
- data/lib/thrift/serializer/deserializer.rb +33 -0
- data/lib/thrift/serializer/serializer.rb +34 -0
- data/lib/thrift/server/base_server.rb +31 -0
- data/lib/thrift/server/mongrel_http_server.rb +58 -0
- data/lib/thrift/server/nonblocking_server.rb +305 -0
- data/lib/thrift/server/simple_server.rb +43 -0
- data/lib/thrift/server/thread_pool_server.rb +75 -0
- data/lib/thrift/server/threaded_server.rb +47 -0
- data/lib/thrift/struct.rb +237 -0
- data/lib/thrift/struct_union.rb +192 -0
- data/lib/thrift/thrift_native.rb +24 -0
- data/lib/thrift/transport/base_server_transport.rb +37 -0
- data/lib/thrift/transport/base_transport.rb +107 -0
- data/lib/thrift/transport/buffered_transport.rb +108 -0
- data/lib/thrift/transport/framed_transport.rb +116 -0
- data/lib/thrift/transport/http_client_transport.rb +51 -0
- data/lib/thrift/transport/io_stream_transport.rb +39 -0
- data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
- data/lib/thrift/transport/server_socket.rb +63 -0
- data/lib/thrift/transport/socket.rb +137 -0
- data/lib/thrift/transport/unix_server_socket.rb +60 -0
- data/lib/thrift/transport/unix_socket.rb +40 -0
- data/lib/thrift/types.rb +101 -0
- data/lib/thrift/union.rb +179 -0
- data/spec/ThriftSpec.thrift +132 -0
- data/spec/base_protocol_spec.rb +160 -0
- data/spec/base_transport_spec.rb +351 -0
- data/spec/binary_protocol_accelerated_spec.rb +46 -0
- data/spec/binary_protocol_spec.rb +61 -0
- data/spec/binary_protocol_spec_shared.rb +375 -0
- data/spec/client_spec.rb +100 -0
- data/spec/compact_protocol_spec.rb +144 -0
- data/spec/exception_spec.rb +142 -0
- data/spec/http_client_spec.rb +64 -0
- data/spec/mongrel_http_server_spec.rb +117 -0
- data/spec/nonblocking_server_spec.rb +265 -0
- data/spec/processor_spec.rb +83 -0
- data/spec/serializer_spec.rb +69 -0
- data/spec/server_socket_spec.rb +80 -0
- data/spec/server_spec.rb +159 -0
- data/spec/socket_spec.rb +61 -0
- data/spec/socket_spec_shared.rb +104 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/struct_spec.rb +295 -0
- data/spec/types_spec.rb +116 -0
- data/spec/union_spec.rb +193 -0
- data/spec/unix_socket_spec.rb +108 -0
- metadata +247 -0
data/lib/thrift/types.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
#
|
2
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
# or more contributor license agreements. See the NOTICE file
|
4
|
+
# distributed with this work for additional information
|
5
|
+
# regarding copyright ownership. The ASF licenses this file
|
6
|
+
# to you under the Apache License, Version 2.0 (the
|
7
|
+
# "License"); you may not use this file except in compliance
|
8
|
+
# with the License. You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing,
|
13
|
+
# software distributed under the License is distributed on an
|
14
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
# KIND, either express or implied. See the License for the
|
16
|
+
# specific language governing permissions and limitations
|
17
|
+
# under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'set'
|
21
|
+
|
22
|
+
module Thrift
|
23
|
+
module Types
|
24
|
+
STOP = 0
|
25
|
+
VOID = 1
|
26
|
+
BOOL = 2
|
27
|
+
BYTE = 3
|
28
|
+
DOUBLE = 4
|
29
|
+
I16 = 6
|
30
|
+
I32 = 8
|
31
|
+
I64 = 10
|
32
|
+
STRING = 11
|
33
|
+
STRUCT = 12
|
34
|
+
MAP = 13
|
35
|
+
SET = 14
|
36
|
+
LIST = 15
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self
|
40
|
+
attr_accessor :type_checking
|
41
|
+
end
|
42
|
+
|
43
|
+
class TypeError < Exception
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.check_type(value, field, name, skip_nil=true)
|
47
|
+
return if value.nil? and skip_nil
|
48
|
+
klasses = case field[:type]
|
49
|
+
when Types::VOID
|
50
|
+
NilClass
|
51
|
+
when Types::BOOL
|
52
|
+
[TrueClass, FalseClass]
|
53
|
+
when Types::BYTE, Types::I16, Types::I32, Types::I64
|
54
|
+
Integer
|
55
|
+
when Types::DOUBLE
|
56
|
+
Float
|
57
|
+
when Types::STRING
|
58
|
+
String
|
59
|
+
when Types::STRUCT
|
60
|
+
[Struct, Union]
|
61
|
+
when Types::MAP
|
62
|
+
Hash
|
63
|
+
when Types::SET
|
64
|
+
Set
|
65
|
+
when Types::LIST
|
66
|
+
Array
|
67
|
+
end
|
68
|
+
valid = klasses && [*klasses].any? { |klass| klass === value }
|
69
|
+
raise TypeError, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid
|
70
|
+
# check elements now
|
71
|
+
case field[:type]
|
72
|
+
when Types::MAP
|
73
|
+
value.each_pair do |k,v|
|
74
|
+
check_type(k, field[:key], "#{name}.key", false)
|
75
|
+
check_type(v, field[:value], "#{name}.value", false)
|
76
|
+
end
|
77
|
+
when Types::SET, Types::LIST
|
78
|
+
value.each do |el|
|
79
|
+
check_type(el, field[:element], "#{name}.element", false)
|
80
|
+
end
|
81
|
+
when Types::STRUCT
|
82
|
+
raise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.class
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.type_name(type)
|
87
|
+
Types.constants.each do |const|
|
88
|
+
return "Types::#{const}" if Types.const_get(const) == type
|
89
|
+
end
|
90
|
+
nil
|
91
|
+
end
|
92
|
+
|
93
|
+
module MessageTypes
|
94
|
+
CALL = 1
|
95
|
+
REPLY = 2
|
96
|
+
EXCEPTION = 3
|
97
|
+
ONEWAY = 4
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
Thrift.type_checking = false if Thrift.type_checking.nil?
|
data/lib/thrift/union.rb
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
#
|
2
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
# or more contributor license agreements. See the NOTICE file
|
4
|
+
# distributed with this work for additional information
|
5
|
+
# regarding copyright ownership. The ASF licenses this file
|
6
|
+
# to you under the Apache License, Version 2.0 (the
|
7
|
+
# "License"); you may not use this file except in compliance
|
8
|
+
# with the License. You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing,
|
13
|
+
# software distributed under the License is distributed on an
|
14
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
# KIND, either express or implied. See the License for the
|
16
|
+
# specific language governing permissions and limitations
|
17
|
+
# under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Thrift
|
21
|
+
class Union
|
22
|
+
def initialize(name=nil, value=nil)
|
23
|
+
if name
|
24
|
+
if name.is_a? Hash
|
25
|
+
if name.size > 1
|
26
|
+
raise "#{self.class} cannot be instantiated with more than one field!"
|
27
|
+
end
|
28
|
+
|
29
|
+
name, value = name.keys.first, name.values.first
|
30
|
+
end
|
31
|
+
|
32
|
+
if Thrift.type_checking
|
33
|
+
raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
if value.nil?
|
37
|
+
raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!"
|
38
|
+
end
|
39
|
+
|
40
|
+
Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking
|
41
|
+
elsif !value.nil?
|
42
|
+
raise Exception, "Value provided, but no name!"
|
43
|
+
end
|
44
|
+
@setfield = name
|
45
|
+
@value = value
|
46
|
+
end
|
47
|
+
|
48
|
+
def inspect
|
49
|
+
if get_set_field
|
50
|
+
"<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
|
51
|
+
else
|
52
|
+
"<#{self.class} >"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def read(iprot)
|
57
|
+
iprot.read_struct_begin
|
58
|
+
fname, ftype, fid = iprot.read_field_begin
|
59
|
+
handle_message(iprot, fid, ftype)
|
60
|
+
iprot.read_field_end
|
61
|
+
|
62
|
+
fname, ftype, fid = iprot.read_field_begin
|
63
|
+
raise "Too many fields for union" unless (ftype == Types::STOP)
|
64
|
+
|
65
|
+
iprot.read_struct_end
|
66
|
+
validate
|
67
|
+
end
|
68
|
+
|
69
|
+
def write(oprot)
|
70
|
+
validate
|
71
|
+
oprot.write_struct_begin(self.class.name)
|
72
|
+
|
73
|
+
fid = self.name_to_id(@setfield.to_s)
|
74
|
+
|
75
|
+
field_info = struct_fields[fid]
|
76
|
+
type = field_info[:type]
|
77
|
+
if is_container? type
|
78
|
+
oprot.write_field_begin(@setfield, type, fid)
|
79
|
+
write_container(oprot, @value, field_info)
|
80
|
+
oprot.write_field_end
|
81
|
+
else
|
82
|
+
oprot.write_field(@setfield, type, fid, @value)
|
83
|
+
end
|
84
|
+
|
85
|
+
oprot.write_field_stop
|
86
|
+
oprot.write_struct_end
|
87
|
+
end
|
88
|
+
|
89
|
+
def ==(other)
|
90
|
+
other != nil && @setfield == other.get_set_field && @value == other.get_value
|
91
|
+
end
|
92
|
+
|
93
|
+
def eql?(other)
|
94
|
+
self.class == other.class && self == other
|
95
|
+
end
|
96
|
+
|
97
|
+
def hash
|
98
|
+
[self.class.name, @setfield, @value].hash
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.field_accessor(klass, field_info)
|
102
|
+
klass.send :define_method, field_info[:name] do
|
103
|
+
if field_info[:name].to_sym == @setfield
|
104
|
+
@value
|
105
|
+
else
|
106
|
+
raise RuntimeError, "#{field_info[:name]} is not union's set field."
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
klass.send :define_method, "#{field_info[:name]}=" do |value|
|
111
|
+
Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
|
112
|
+
@setfield = field_info[:name].to_sym
|
113
|
+
@value = value
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.qmark_isset_method(klass, field_info)
|
118
|
+
klass.send :define_method, "#{field_info[:name]}?" do
|
119
|
+
get_set_field == field_info[:name].to_sym && !get_value.nil?
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.generate_accessors(klass)
|
124
|
+
klass::FIELDS.values.each do |field_info|
|
125
|
+
field_accessor(klass, field_info)
|
126
|
+
qmark_isset_method(klass, field_info)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# get the symbol that indicates what the currently set field type is.
|
131
|
+
def get_set_field
|
132
|
+
@setfield
|
133
|
+
end
|
134
|
+
|
135
|
+
# get the current value of this union, regardless of what the set field is.
|
136
|
+
# generally, you should only use this method when you don't know in advance
|
137
|
+
# what field to expect.
|
138
|
+
def get_value
|
139
|
+
@value
|
140
|
+
end
|
141
|
+
|
142
|
+
def <=>(other)
|
143
|
+
if self.class == other.class
|
144
|
+
if get_set_field == other.get_set_field
|
145
|
+
if get_set_field.nil?
|
146
|
+
0
|
147
|
+
else
|
148
|
+
get_value <=> other.get_value
|
149
|
+
end
|
150
|
+
else
|
151
|
+
if get_set_field && other.get_set_field.nil?
|
152
|
+
-1
|
153
|
+
elsif get_set_field.nil? && other.get_set_field
|
154
|
+
1
|
155
|
+
elsif get_set_field.nil? && other.get_set_field.nil?
|
156
|
+
0
|
157
|
+
else
|
158
|
+
name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
else
|
162
|
+
self.class <=> other.class
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
protected
|
167
|
+
|
168
|
+
def handle_message(iprot, fid, ftype)
|
169
|
+
field = struct_fields[fid]
|
170
|
+
if field and field[:type] == ftype
|
171
|
+
@value = read_field(iprot, field)
|
172
|
+
name = field[:name].to_sym
|
173
|
+
@setfield = name
|
174
|
+
else
|
175
|
+
iprot.skip(ftype)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
/*
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
|
20
|
+
#
|
21
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
22
|
+
# or more contributor license agreements. See the NOTICE file
|
23
|
+
# distributed with this work for additional information
|
24
|
+
# regarding copyright ownership. The ASF licenses this file
|
25
|
+
# to you under the Apache License, Version 2.0 (the
|
26
|
+
# "License"); you may not use this file except in compliance
|
27
|
+
# with the License. You may obtain a copy of the License at
|
28
|
+
#
|
29
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
30
|
+
#
|
31
|
+
# Unless required by applicable law or agreed to in writing,
|
32
|
+
# software distributed under the License is distributed on an
|
33
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
34
|
+
# KIND, either express or implied. See the License for the
|
35
|
+
# specific language governing permissions and limitations
|
36
|
+
# under the License.
|
37
|
+
#
|
38
|
+
|
39
|
+
namespace rb SpecNamespace
|
40
|
+
|
41
|
+
struct Hello {
|
42
|
+
1: string greeting = "hello world"
|
43
|
+
}
|
44
|
+
|
45
|
+
enum SomeEnum {
|
46
|
+
ONE
|
47
|
+
TWO
|
48
|
+
}
|
49
|
+
|
50
|
+
struct StructWithSomeEnum {
|
51
|
+
1: SomeEnum some_enum;
|
52
|
+
}
|
53
|
+
|
54
|
+
union TestUnion {
|
55
|
+
/**
|
56
|
+
* A doc string
|
57
|
+
*/
|
58
|
+
1: string string_field;
|
59
|
+
2: i32 i32_field;
|
60
|
+
3: i32 other_i32_field;
|
61
|
+
4: SomeEnum enum_field;
|
62
|
+
5: binary binary_field;
|
63
|
+
}
|
64
|
+
|
65
|
+
struct Foo {
|
66
|
+
1: i32 simple = 53,
|
67
|
+
2: string words = "words",
|
68
|
+
3: Hello hello = {'greeting' : "hello, world!"},
|
69
|
+
4: list<i32> ints = [1, 2, 2, 3],
|
70
|
+
5: map<i32, map<string, double>> complex,
|
71
|
+
6: set<i16> shorts = [5, 17, 239],
|
72
|
+
7: optional string opt_string
|
73
|
+
8: bool my_bool
|
74
|
+
}
|
75
|
+
|
76
|
+
struct Foo2 {
|
77
|
+
1: binary my_binary
|
78
|
+
}
|
79
|
+
|
80
|
+
struct BoolStruct {
|
81
|
+
1: bool yesno = 1
|
82
|
+
}
|
83
|
+
|
84
|
+
struct SimpleList {
|
85
|
+
1: list<bool> bools,
|
86
|
+
2: list<byte> bytes,
|
87
|
+
3: list<i16> i16s,
|
88
|
+
4: list<i32> i32s,
|
89
|
+
5: list<i64> i64s,
|
90
|
+
6: list<double> doubles,
|
91
|
+
7: list<string> strings,
|
92
|
+
8: list<map<i16, i16>> maps,
|
93
|
+
9: list<list<i16>> lists,
|
94
|
+
10: list<set<i16>> sets,
|
95
|
+
11: list<Hello> hellos
|
96
|
+
}
|
97
|
+
|
98
|
+
exception Xception {
|
99
|
+
1: string message,
|
100
|
+
2: i32 code = 1
|
101
|
+
}
|
102
|
+
|
103
|
+
service NonblockingService {
|
104
|
+
Hello greeting(1:bool english)
|
105
|
+
bool block()
|
106
|
+
oneway void unblock(1:i32 n)
|
107
|
+
oneway void shutdown()
|
108
|
+
void sleep(1:double seconds)
|
109
|
+
}
|
110
|
+
|
111
|
+
union My_union {
|
112
|
+
1: bool im_true,
|
113
|
+
2: byte a_bite,
|
114
|
+
3: i16 integer16,
|
115
|
+
4: i32 integer32,
|
116
|
+
5: i64 integer64,
|
117
|
+
6: double double_precision,
|
118
|
+
7: string some_characters,
|
119
|
+
8: i32 other_i32
|
120
|
+
9: SomeEnum some_enum;
|
121
|
+
10: map<SomeEnum, list<SomeEnum>> my_map;
|
122
|
+
}
|
123
|
+
|
124
|
+
struct Struct_with_union {
|
125
|
+
1: My_union fun_union
|
126
|
+
2: i32 integer32
|
127
|
+
3: string some_characters
|
128
|
+
}
|
129
|
+
|
130
|
+
struct StructWithEnumMap {
|
131
|
+
1: map<SomeEnum, list<SomeEnum>> my_map;
|
132
|
+
}
|
@@ -0,0 +1,160 @@
|
|
1
|
+
#
|
2
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
# or more contributor license agreements. See the NOTICE file
|
4
|
+
# distributed with this work for additional information
|
5
|
+
# regarding copyright ownership. The ASF licenses this file
|
6
|
+
# to you under the Apache License, Version 2.0 (the
|
7
|
+
# "License"); you may not use this file except in compliance
|
8
|
+
# with the License. You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing,
|
13
|
+
# software distributed under the License is distributed on an
|
14
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
# KIND, either express or implied. See the License for the
|
16
|
+
# specific language governing permissions and limitations
|
17
|
+
# under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
|
21
|
+
|
22
|
+
class ThriftBaseProtocolSpec < Spec::ExampleGroup
|
23
|
+
include Thrift
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
@trans = mock("MockTransport")
|
27
|
+
@prot = BaseProtocol.new(@trans)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe BaseProtocol do
|
31
|
+
# most of the methods are stubs, so we can ignore them
|
32
|
+
|
33
|
+
it "should make trans accessible" do
|
34
|
+
@prot.trans.should eql(@trans)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should write out a field nicely" do
|
38
|
+
@prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
|
39
|
+
@prot.should_receive(:write_type).with('type', 'value').ordered
|
40
|
+
@prot.should_receive(:write_field_end).ordered
|
41
|
+
@prot.write_field('field', 'type', 'fid', 'value')
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should write out the different types" do
|
45
|
+
@prot.should_receive(:write_bool).with('bool').ordered
|
46
|
+
@prot.should_receive(:write_byte).with('byte').ordered
|
47
|
+
@prot.should_receive(:write_double).with('double').ordered
|
48
|
+
@prot.should_receive(:write_i16).with('i16').ordered
|
49
|
+
@prot.should_receive(:write_i32).with('i32').ordered
|
50
|
+
@prot.should_receive(:write_i64).with('i64').ordered
|
51
|
+
@prot.should_receive(:write_string).with('string').ordered
|
52
|
+
struct = mock('Struct')
|
53
|
+
struct.should_receive(:write).with(@prot).ordered
|
54
|
+
@prot.write_type(Types::BOOL, 'bool')
|
55
|
+
@prot.write_type(Types::BYTE, 'byte')
|
56
|
+
@prot.write_type(Types::DOUBLE, 'double')
|
57
|
+
@prot.write_type(Types::I16, 'i16')
|
58
|
+
@prot.write_type(Types::I32, 'i32')
|
59
|
+
@prot.write_type(Types::I64, 'i64')
|
60
|
+
@prot.write_type(Types::STRING, 'string')
|
61
|
+
@prot.write_type(Types::STRUCT, struct)
|
62
|
+
# all other types are not implemented
|
63
|
+
[Types::STOP, Types::VOID, Types::MAP, Types::SET, Types::LIST].each do |type|
|
64
|
+
lambda { @prot.write_type(type, type.to_s) }.should raise_error(NotImplementedError)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should read the different types" do
|
69
|
+
@prot.should_receive(:read_bool).ordered
|
70
|
+
@prot.should_receive(:read_byte).ordered
|
71
|
+
@prot.should_receive(:read_i16).ordered
|
72
|
+
@prot.should_receive(:read_i32).ordered
|
73
|
+
@prot.should_receive(:read_i64).ordered
|
74
|
+
@prot.should_receive(:read_double).ordered
|
75
|
+
@prot.should_receive(:read_string).ordered
|
76
|
+
@prot.read_type(Types::BOOL)
|
77
|
+
@prot.read_type(Types::BYTE)
|
78
|
+
@prot.read_type(Types::I16)
|
79
|
+
@prot.read_type(Types::I32)
|
80
|
+
@prot.read_type(Types::I64)
|
81
|
+
@prot.read_type(Types::DOUBLE)
|
82
|
+
@prot.read_type(Types::STRING)
|
83
|
+
# all other types are not implemented
|
84
|
+
[Types::STOP, Types::VOID, Types::MAP, Types::SET, Types::LIST].each do |type|
|
85
|
+
lambda { @prot.read_type(type) }.should raise_error(NotImplementedError)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should skip the basic types" do
|
90
|
+
@prot.should_receive(:read_bool).ordered
|
91
|
+
@prot.should_receive(:read_byte).ordered
|
92
|
+
@prot.should_receive(:read_i16).ordered
|
93
|
+
@prot.should_receive(:read_i32).ordered
|
94
|
+
@prot.should_receive(:read_i64).ordered
|
95
|
+
@prot.should_receive(:read_double).ordered
|
96
|
+
@prot.should_receive(:read_string).ordered
|
97
|
+
@prot.skip(Types::BOOL)
|
98
|
+
@prot.skip(Types::BYTE)
|
99
|
+
@prot.skip(Types::I16)
|
100
|
+
@prot.skip(Types::I32)
|
101
|
+
@prot.skip(Types::I64)
|
102
|
+
@prot.skip(Types::DOUBLE)
|
103
|
+
@prot.skip(Types::STRING)
|
104
|
+
@prot.skip(Types::STOP) # should do absolutely nothing
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should skip structs" do
|
108
|
+
real_skip = @prot.method(:skip)
|
109
|
+
@prot.should_receive(:read_struct_begin).ordered
|
110
|
+
@prot.should_receive(:read_field_begin).exactly(4).times.and_return(
|
111
|
+
['field 1', Types::STRING, 1],
|
112
|
+
['field 2', Types::I32, 2],
|
113
|
+
['field 3', Types::MAP, 3],
|
114
|
+
[nil, Types::STOP, 0]
|
115
|
+
)
|
116
|
+
@prot.should_receive(:read_field_end).exactly(3).times
|
117
|
+
@prot.should_receive(:read_string).exactly(3).times
|
118
|
+
@prot.should_receive(:read_i32).ordered
|
119
|
+
@prot.should_receive(:read_map_begin).ordered.and_return([Types::STRING, Types::STRING, 1])
|
120
|
+
# @prot.should_receive(:read_string).exactly(2).times
|
121
|
+
@prot.should_receive(:read_map_end).ordered
|
122
|
+
@prot.should_receive(:read_struct_end).ordered
|
123
|
+
real_skip.call(Types::STRUCT)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should skip maps" do
|
127
|
+
real_skip = @prot.method(:skip)
|
128
|
+
@prot.should_receive(:read_map_begin).ordered.and_return([Types::STRING, Types::STRUCT, 1])
|
129
|
+
@prot.should_receive(:read_string).ordered
|
130
|
+
@prot.should_receive(:read_struct_begin).ordered.and_return(["some_struct"])
|
131
|
+
@prot.should_receive(:read_field_begin).ordered.and_return([nil, Types::STOP, nil]);
|
132
|
+
@prot.should_receive(:read_struct_end).ordered
|
133
|
+
@prot.should_receive(:read_map_end).ordered
|
134
|
+
real_skip.call(Types::MAP)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should skip sets" do
|
138
|
+
real_skip = @prot.method(:skip)
|
139
|
+
@prot.should_receive(:read_set_begin).ordered.and_return([Types::I64, 9])
|
140
|
+
@prot.should_receive(:read_i64).ordered.exactly(9).times
|
141
|
+
@prot.should_receive(:read_set_end)
|
142
|
+
real_skip.call(Types::SET)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should skip lists" do
|
146
|
+
real_skip = @prot.method(:skip)
|
147
|
+
@prot.should_receive(:read_list_begin).ordered.and_return([Types::DOUBLE, 11])
|
148
|
+
@prot.should_receive(:read_double).ordered.exactly(11).times
|
149
|
+
@prot.should_receive(:read_list_end)
|
150
|
+
real_skip.call(Types::LIST)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe BaseProtocolFactory do
|
155
|
+
it "should raise NotImplementedError" do
|
156
|
+
# returning nil since Protocol is just an abstract class
|
157
|
+
lambda {BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|