Skip to content

Commit

Permalink
tests are finnicky, but this [hopefully] greatly reduces memory usage…
Browse files Browse the repository at this point in the history
… and improves status update performance.

git-svn-id: https://xmpp4r-simple.googlecode.com/svn/trunk@20 1e3c5029-9921-0410-9e82-436f4d538387
  • Loading branch information
romeda committed Dec 7, 2006
1 parent 06d0da5 commit f5a8983
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
32 changes: 28 additions & 4 deletions lib/xmpp4r-simple.rb
Expand Up @@ -226,11 +226,22 @@ def received_messages?
#
# e.g.:
#
# jabber.presence_updates do |friend, old_presence, new_presence|
# puts "Received presence update from #{friend.to_s}: #{new_presence}"
# jabber.presence_updates do |friend, new_presence|
# puts "Received presence update from #{friend}: #{new_presence}"
# end
def presence_updates(&block)
dequeue(:presence_updates, &block)
updates = []
@presence_mutex.synchronize do
dequeue(:presence_updates) do |friend|
presence = @presence_updates[friend]
next unless presence
new_update = [friend, presence[0], presence[1]]
yield new_update if block_given?
updates << new_update
@presence_updates.delete(friend)
end
end
return updates
end

# Returns true if there are unprocessed presence updates waiting in the
Expand Down Expand Up @@ -384,8 +395,21 @@ def register_default_callbacks
end
end

@presence_updates = {}
@presence_mutex = Mutex.new
roster.add_presence_callback do |roster_item, old_presence, new_presence|
queue(:presence_updates) << [roster_item, old_presence, new_presence]
simple_jid = roster_item.jid.strip.to_s
presence = case new_presence.type
when nil: new_presence.show || :online
when :unavailable: :unavailable
else
nil
end

if presence && @presence_updates[simple_jid] != presence
queue(:presence_updates) << simple_jid
@presence_mutex.synchronize { @presence_updates[simple_jid] = [presence, new_presence.status] }
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions test/test_xmpp4r_simple.rb
Expand Up @@ -145,6 +145,7 @@ def test_sent_message_should_be_received
def test_presence_updates_should_be_received

@client2.add(@client1)
@client1.add(@client2)

assert_before(60) { assert @client2.subscribed_to?(@jid1) }
assert_before(60) { assert_equal 0, @client2.presence_updates.size }
Expand All @@ -158,9 +159,9 @@ def test_presence_updates_should_be_received
end

new_status = new_statuses.first
assert_equal @jid1, new_status[0].jid.strip.to_s
assert_equal "Doing something else.", new_status[2].status
assert_equal :away, new_status[2].show
assert_equal @jid1, new_status[0]
assert_equal "Doing something else.", new_status[2]
assert_equal :away, new_status[1]
end

def test_disable_auto_accept_subscription_requests
Expand Down

0 comments on commit f5a8983

Please sign in to comment.