langis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,232 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'redis/raketasks'
3
+
4
+ REDIS_KEY = 'langis_spec:logs'
5
+
6
+ RESQUE_QUEUE = 'langis_spec_test_queue'
7
+ RESQUE_REDIS_KEY = 'resque:queue:' + RESQUE_QUEUE
8
+ class MyJob
9
+ @queue = RESQUE_QUEUE
10
+ def perform(*args)
11
+ # Do nothing
12
+ end
13
+ end
14
+
15
+ # A helper class to test transforms
16
+ class Transformable
17
+ def initialize(message, args)
18
+ @message = message
19
+ @args = args
20
+ end
21
+
22
+ def transformer(*args)
23
+ raise 'Transform Arguments mismatch' unless @args == args
24
+ return @message
25
+ end
26
+ end
27
+
28
+ describe 'Redis' do
29
+ before :each do
30
+ begin
31
+ @redis = Redis.new :db => 15
32
+ Resque.redis = @redis
33
+ @redis.flushdb
34
+ rescue Errno::ECONNREFUSED
35
+ raise <<-EOS
36
+
37
+ Cannot connect to Redis.
38
+
39
+ Make sure Redis is running on localhost, port 6379.
40
+ This testing suite connects to the database 15.
41
+
42
+ redis-server spec/redis.conf
43
+
44
+ EOS
45
+ end
46
+ end
47
+
48
+ describe 'normal sink' do
49
+ it 'should add the message to a redis key, from the default MESSAGE_KEY' do
50
+ my_message = 'Hello World'
51
+ env = {
52
+ Langis::MESSAGE_KEY => my_message
53
+ }
54
+ sink = Langis::Sinks.redis @redis, REDIS_KEY
55
+ sink.call(env)
56
+ messages = @redis.lrange REDIS_KEY, 0, -1
57
+ messages.size.should eql 1
58
+ messages[0].should eql my_message
59
+ end
60
+
61
+ it 'should add the message to a redis key, from an alt key' do
62
+ my_key = 'mycustomkey'
63
+ my_message = 'Hello World Custom Key'
64
+ env = {
65
+ my_key => my_message
66
+ }
67
+ sink = Langis::Sinks.redis @redis, REDIS_KEY, :env_key => my_key
68
+ sink.call(env)
69
+ messages = @redis.lrange REDIS_KEY, 0, -1
70
+ messages.size.should eql 1
71
+ messages[0].should eql my_message
72
+ end
73
+
74
+ it 'should add a transformed message to a redis key' do
75
+ transform_message = "Hello Transformed World"
76
+ transform_arguments = [1,2,3,4]
77
+ my_message = Transformable.new(
78
+ transform_message,
79
+ transform_arguments)
80
+ env = {
81
+ Langis::MESSAGE_KEY => my_message
82
+ }
83
+ sink = Langis::Sinks.redis(@redis, REDIS_KEY,
84
+ :transform => :transformer,
85
+ :transform_args => transform_arguments)
86
+ sink.call(env)
87
+ messages = @redis.lrange REDIS_KEY, 0, -1
88
+ messages.size.should eql 1
89
+ messages[0].should eql transform_message
90
+ end
91
+
92
+ it 'should add a transformed message to a redis key, nil args' do
93
+ transform_message = "Hello Transformed World"
94
+ transform_arguments = []
95
+ my_message = Transformable.new(
96
+ transform_message,
97
+ transform_arguments)
98
+ env = {
99
+ Langis::MESSAGE_KEY => my_message
100
+ }
101
+ sink = Langis::Sinks.redis(@redis, REDIS_KEY,
102
+ :transform => :transformer,
103
+ :transform_args => nil)
104
+ sink.call(env)
105
+ messages = @redis.lrange REDIS_KEY, 0, -1
106
+ messages.size.should eql 1
107
+ messages[0].should eql transform_message
108
+ end
109
+
110
+ it 'should add a transformed message to a redis key, without args' do
111
+ transform_message = "Hello Transformed World"
112
+ transform_arguments = []
113
+ my_message = Transformable.new(
114
+ transform_message,
115
+ transform_arguments)
116
+ env = {
117
+ Langis::MESSAGE_KEY => my_message
118
+ }
119
+ sink = Langis::Sinks.redis(@redis, REDIS_KEY,
120
+ :transform => :transformer)
121
+ sink.call(env)
122
+ messages = @redis.lrange REDIS_KEY, 0, -1
123
+ messages.size.should eql 1
124
+ messages[0].should eql transform_message
125
+ end
126
+ end
127
+
128
+ describe 'Resque Sink' do
129
+ it 'should add the message as a job to a resque queue, from default key' do
130
+ my_message = 'Hello World Resque Job'
131
+ env = {
132
+ Langis::MESSAGE_KEY => my_message
133
+ }
134
+ sink = Langis::Sinks.resque MyJob
135
+ sink.call(env)
136
+ messages = @redis.lrange RESQUE_REDIS_KEY, 0, -1
137
+ messages.size.should eql 1
138
+ job_hash = JSON.parse messages[0]
139
+ job_hash.should be_a_kind_of Hash
140
+ job_hash['class'].should eql MyJob.to_s
141
+ job_hash['args'].should be_a_kind_of Array
142
+ job_hash['args'].size.should eql 1
143
+ job_hash['args'][0].should eql my_message
144
+ end
145
+
146
+ it 'should add the message as a job to a resque queue, from an alt key' do
147
+ my_key = 'mycustomkey'
148
+ my_message = 'Hello World Resque Job Alternate Key'
149
+ env = {
150
+ my_key => my_message
151
+ }
152
+ sink = Langis::Sinks.resque MyJob, :env_key => my_key
153
+ sink.call(env)
154
+ messages = @redis.lrange RESQUE_REDIS_KEY, 0, -1
155
+ messages.size.should eql 1
156
+ job_hash = JSON.parse messages[0]
157
+ job_hash.should be_a_kind_of Hash
158
+ job_hash['class'].should eql MyJob.to_s
159
+ job_hash['args'].should be_a_kind_of Array
160
+ job_hash['args'].size.should eql 1
161
+ job_hash['args'][0].should eql my_message
162
+ end
163
+
164
+ it 'should add the transformed message to a resque queue' do
165
+ transform_message = "Hello Transformed World"
166
+ transform_arguments = [1,2,3,4]
167
+ my_message = Transformable.new(
168
+ transform_message,
169
+ transform_arguments)
170
+ env = {
171
+ Langis::MESSAGE_KEY => my_message
172
+ }
173
+ sink = Langis::Sinks.resque(MyJob,
174
+ :transform => :transformer,
175
+ :transform_args => transform_arguments)
176
+ sink.call(env)
177
+ messages = @redis.lrange RESQUE_REDIS_KEY, 0, -1
178
+ messages.size.should eql 1
179
+ job_hash = JSON.parse messages[0]
180
+ job_hash.should be_a_kind_of Hash
181
+ job_hash['class'].should eql MyJob.to_s
182
+ job_hash['args'].should be_a_kind_of Array
183
+ job_hash['args'].size.should eql 1
184
+ job_hash['args'][0].should eql transform_message
185
+ end
186
+
187
+ it 'should add the transformed message to a resque queue, nil args' do
188
+ transform_message = "Hello Transformed World"
189
+ transform_arguments = []
190
+ my_message = Transformable.new(
191
+ transform_message,
192
+ transform_arguments)
193
+ env = {
194
+ Langis::MESSAGE_KEY => my_message
195
+ }
196
+ sink = Langis::Sinks.resque(MyJob,
197
+ :transform => :transformer,
198
+ :transform_args => nil)
199
+ sink.call(env)
200
+ messages = @redis.lrange RESQUE_REDIS_KEY, 0, -1
201
+ messages.size.should eql 1
202
+ job_hash = JSON.parse messages[0]
203
+ job_hash.should be_a_kind_of Hash
204
+ job_hash['class'].should eql MyJob.to_s
205
+ job_hash['args'].should be_a_kind_of Array
206
+ job_hash['args'].size.should eql 1
207
+ job_hash['args'][0].should eql transform_message
208
+ end
209
+
210
+ it 'should add the transformed message to a resque queue, without args' do
211
+ transform_message = "Hello Transformed World"
212
+ transform_arguments = []
213
+ my_message = Transformable.new(
214
+ transform_message,
215
+ transform_arguments)
216
+ env = {
217
+ Langis::MESSAGE_KEY => my_message
218
+ }
219
+ sink = Langis::Sinks.resque(MyJob,
220
+ :transform => :transformer)
221
+ sink.call(env)
222
+ messages = @redis.lrange RESQUE_REDIS_KEY, 0, -1
223
+ messages.size.should eql 1
224
+ job_hash = JSON.parse messages[0]
225
+ job_hash.should be_a_kind_of Hash
226
+ job_hash['class'].should eql MyJob.to_s
227
+ job_hash['args'].should be_a_kind_of Array
228
+ job_hash['args'].size.should eql 1
229
+ job_hash['args'][0].should eql transform_message
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,132 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize no
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile redis.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 6379
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ #save 900 1
34
+ #save 300 10
35
+ #save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename spec_test_redis_db.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ #dir /opt/local/var/db/redis
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
116
+
117
+ # Use object sharing. Can save a lot of memory if you have many common
118
+ # string in your dataset, but performs lookups against the shared objects
119
+ # pool so it uses more CPU and can be a bit slower. Usually it's a good
120
+ # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
131
+ shareobjects no
132
+ # shareobjectspoolsize 1024
@@ -0,0 +1,8 @@
1
+ # Load in all dependent libs using Bundler
2
+ require 'bundler'
3
+ Bundler.setup
4
+ Bundler.require :default, :spec
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'langis'
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: langis
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Benjamin Yu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-29 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: blockenspiel
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: eventmachine
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 2
56
+ - 9
57
+ version: 1.2.9
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: yard
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: blockenspiel
76
+ requirement: &id005 !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: *id005
87
+ - !ruby/object:Gem::Dependency
88
+ name: eventmachine
89
+ requirement: &id006 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ type: :runtime
98
+ prerelease: false
99
+ version_requirements: *id006
100
+ description: "\n Langis is a Rack inspired publish-subscribe system for Ruby.\n It has flexible message routing and message handling using a custom\n Domain Specific Language and a Rack-inspired message handler framework.\n This can give Rails applications better (and decoupled) visibility\n and management of the background processes it creates (or executes)\n in response to its actions (in controllers or models).\n "
101
+ email: benjaminlyu@gmail.com
102
+ executables: []
103
+
104
+ extensions: []
105
+
106
+ extra_rdoc_files:
107
+ - LICENSE
108
+ - README.md
109
+ files:
110
+ - Gemfile
111
+ - LICENSE
112
+ - NOTICE
113
+ - README.md
114
+ - Rakefile
115
+ - VERSION
116
+ - generators/langis_config_generator.rb
117
+ - generators/templates/langis_config.rb
118
+ - lib/langis.rb
119
+ - lib/langis/dsl.rb
120
+ - lib/langis/engine.rb
121
+ - lib/langis/middleware.rb
122
+ - lib/langis/rackish.rb
123
+ - lib/langis/sinks.rb
124
+ - spec/langis/dsl_spec.rb
125
+ - spec/langis/engine_spec.rb
126
+ - spec/langis/middleware_spec.rb
127
+ - spec/langis/rackish_spec.rb
128
+ - spec/langis/sinks/delayed_job_sink_spec.rb
129
+ - spec/langis/sinks/redis_resque_sink_spec.rb
130
+ - spec/redis.conf
131
+ - spec/spec_helper.rb
132
+ has_rdoc: true
133
+ homepage: http://github.com/byu/langis
134
+ licenses: []
135
+
136
+ post_install_message:
137
+ rdoc_options: []
138
+
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ segments:
155
+ - 0
156
+ version: "0"
157
+ requirements: []
158
+
159
+ rubyforge_project:
160
+ rubygems_version: 1.3.7
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Langis is a Rack inspired publish-subscribe system for Ruby.
164
+ test_files:
165
+ - spec/langis/dsl_spec.rb
166
+ - spec/langis/engine_spec.rb
167
+ - spec/langis/middleware_spec.rb
168
+ - spec/langis/rackish_spec.rb
169
+ - spec/langis/sinks/delayed_job_sink_spec.rb
170
+ - spec/langis/sinks/redis_resque_sink_spec.rb
171
+ - spec/spec_helper.rb