kafka-rb 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -44,6 +44,7 @@ sudo gem install kafka-rb
44
44
  consumer = Kafka::Consumer.new
45
45
  messages = consumer.consume
46
46
 
47
+
47
48
  ### Consuming messages using a block loop
48
49
 
49
50
  require 'kafka'
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake/gempackagetask'
3
3
  require 'rubygems/specification'
4
4
  require 'date'
5
- require 'spec/rake/spectask'
5
+ require 'rspec/core/rake_task'
6
6
 
7
7
  GEM = 'kafka-rb'
8
8
  GEM_NAME = 'Kafka Client'
@@ -33,9 +33,9 @@ end
33
33
  task :default => :spec
34
34
 
35
35
  desc "Run specs"
36
- Spec::Rake::SpecTask.new do |t|
37
- t.spec_files = FileList['spec/**/*_spec.rb']
38
- t.spec_opts = %w(-fs --color)
36
+ RSpec::Core::RakeTask.new do |t|
37
+ t.pattern = FileList['spec/**/*_spec.rb']
38
+ t.rspec_opts = %w(-fs --color)
39
39
  end
40
40
 
41
41
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -55,7 +55,7 @@ task :make_spec do
55
55
  end
56
56
 
57
57
  desc "Run all examples with RCov"
58
- Spec::Rake::SpecTask.new(:rcov) do |t|
59
- t.spec_files = FileList['spec/**/*_spec.rb']
58
+ RSpec::Core::RakeTask.new(:rcov) do |t|
59
+ t.pattern = FileList['spec/**/*_spec.rb']
60
60
  t.rcov = true
61
61
  end
@@ -71,9 +71,11 @@ module Kafka
71
71
  processed = 0
72
72
  length = data.length - 4
73
73
  while(processed <= length) do
74
- message_size = data[processed, 4].unpack("N").shift
75
- messages << Kafka::Message.parse_from(data[processed, message_size + 4])
76
- processed += 4 + message_size
74
+ message_size = data[processed, 4].unpack("N").shift + 4
75
+ message_data = data[processed, message_size]
76
+ break unless message_data.size == message_size
77
+ messages << Kafka::Message.parse_from(message_data)
78
+ processed += message_size
77
79
  end
78
80
  self.offset += processed
79
81
  messages
@@ -16,7 +16,12 @@ module Kafka
16
16
  end
17
17
 
18
18
  def encode(message)
19
- [message.magic].pack("C") + [message.calculate_checksum].pack("N") + message.payload.to_s
19
+ if RUBY_VERSION[0,3] == "1.8" # Use old iconv on Ruby 1.8 for encoding
20
+ ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
21
+ [message.magic].pack("C") + [message.calculate_checksum].pack("N") + ic.iconv(message.payload.to_s)
22
+ else
23
+ [message.magic].pack("C") + [message.calculate_checksum].pack("N") + message.payload.to_s.force_encoding(Encoding::ASCII_8BIT)
24
+ end
20
25
  end
21
26
 
22
27
  def encode_request(topic, partition, messages)
data/lib/kafka.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'socket'
2
2
  require 'zlib'
3
+ if RUBY_VERSION[0,3] == "1.8"
4
+ require 'iconv'
5
+ end
3
6
 
4
7
  require File.join(File.dirname(__FILE__), "kafka", "io")
5
8
  require File.join(File.dirname(__FILE__), "kafka", "request_type")
@@ -64,7 +64,7 @@ describe Consumer do
64
64
  end
65
65
 
66
66
  it "should read the response data" do
67
- bytes = [12].pack("N") + [0].pack("C") + [1120192889].pack("N") + "ale"
67
+ bytes = [8].pack("N") + [0].pack("C") + [1120192889].pack("N") + "ale"
68
68
  @mocked_socket.should_receive(:read).exactly(:twice).and_return(bytes)
69
69
  @consumer.read_data_response.should eql(bytes[2, bytes.length])
70
70
  end
@@ -78,7 +78,7 @@ describe Consumer do
78
78
  end
79
79
 
80
80
  it "should parse a message set from bytes" do
81
- bytes = [12].pack("N") + [0].pack("C") + [1120192889].pack("N") + "ale"
81
+ bytes = [8].pack("N") + [0].pack("C") + [1120192889].pack("N") + "ale"
82
82
  message = @consumer.parse_message_set_from(bytes).first
83
83
  message.payload.should eql("ale")
84
84
  message.checksum.should eql(1120192889)
@@ -86,6 +86,30 @@ describe Consumer do
86
86
  message.valid?.should eql(true)
87
87
  end
88
88
 
89
+ it "should skip an incomplete message at the end of the response" do
90
+ bytes = [8].pack("N") + [0].pack("C") + [1120192889].pack("N") + "ale"
91
+ # incomplete message
92
+ bytes += [8].pack("N")
93
+ messages = @consumer.parse_message_set_from(bytes)
94
+ messages.size.should eql(1)
95
+ end
96
+
97
+ it "should skip an incomplete message at the end of the response which has the same length as an empty message" do
98
+ bytes = [8].pack("N") + [0].pack("C") + [1120192889].pack("N") + "ale"
99
+ # incomplete message because payload is missing
100
+ bytes += [8].pack("N") + [0].pack("C") + [1120192889].pack("N")
101
+ messages = @consumer.parse_message_set_from(bytes)
102
+ messages.size.should eql(1)
103
+ end
104
+
105
+ it "should read empty messages correctly" do
106
+ # empty message
107
+ bytes = [5].pack("N") + [0].pack("C") + [0].pack("N") + ""
108
+ messages = @consumer.parse_message_set_from(bytes)
109
+ messages.size.should eql(1)
110
+ messages.first.payload.should eql("")
111
+ end
112
+
89
113
  it "should consume messages" do
90
114
  @consumer.should_receive(:send_consume_request).and_return(true)
91
115
  @consumer.should_receive(:read_data_response).and_return("")
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require File.dirname(__FILE__) + '/spec_helper'
2
3
 
3
4
  describe Producer do
@@ -44,6 +45,18 @@ describe Producer do
44
45
  full_message = [message.magic].pack("C") + [message.calculate_checksum].pack("N") + message.payload.to_s
45
46
  @producer.encode(message).should eql(full_message)
46
47
  end
48
+
49
+ it "should encode strings containing non-ASCII characters" do
50
+ message = Kafka::Message.new("ümlaut")
51
+ encoded = @producer.encode(message)
52
+ data = [encoded.size].pack("N") + encoded
53
+ if RUBY_VERSION[0,3] == "1.8" # Use old iconv on Ruby 1.8 for encoding
54
+ ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
55
+ ic.iconv(Kafka::Message.parse_from(data).payload).should eql("ümlaut")
56
+ else
57
+ Kafka::Message.parse_from(data).payload.force_encoding(Encoding::ASCII_8BIT).should eql("ümlaut")
58
+ end
59
+ end
47
60
  end
48
61
 
49
62
  describe "Request Encoding" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kafka-rb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alejandro Crosa
@@ -15,7 +15,7 @@ autorequire: kafka-rb
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-14 00:00:00 -08:00
18
+ date: 2011-01-13 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency