* Removed memcaching for TagImplication (too many latent race conditions)

* Added Post.exact_tag_match to skip normalization/metatag parsing
* Added DelayedJob support for tag alias/implication processing
This commit is contained in:
albert
2011-08-04 19:54:13 -04:00
parent 09d7bba90a
commit e106f70b6d
18 changed files with 123 additions and 162 deletions

View File

@@ -1,4 +1,10 @@
Factory.define(:tag_alias) do |f|
f.antecedent_name "aaa"
f.consequent_name "bbb"
FactoryGirl.define do
factory :tag_alias do
antecedent_name "aaa"
consequent_name "bbb"
after_create do |tag_alias|
tag_alias.process!
end
end
end

View File

@@ -1,4 +1,10 @@
Factory.define(:tag_implication) do |f|
f.antecedent_name "aaa"
f.consequent_name "bbb"
FactoryGirl.define do
factory :tag_implication do
antecedent_name "aaa"
consequent_name "bbb"
after_create do |tag_implication|
tag_implication.process!
end
end
end

View File

@@ -7,6 +7,7 @@ class TagAliasesControllerTest < ActionController::TestCase
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
Delayed::Worker.delay_jobs = false
end
teardown do

View File

@@ -7,6 +7,7 @@ class TagImplicationsControllerTest < ActionController::TestCase
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
Delayed::Worker.delay_jobs = false
end
teardown do

View File

@@ -1,4 +1,4 @@
require_relative '../test_helper'
require 'test_helper'
class TagAliasTest < ActiveSupport::TestCase
context "A tag alias" do
@@ -15,14 +15,14 @@ class TagAliasTest < ActiveSupport::TestCase
end
should "populate the creator information" do
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
ta = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_equal(CurrentUser.user.id, ta.creator_id)
end
should "convert a tag to its normalized version" do
tag1 = Factory.create(:tag, :name => "aaa")
tag2 = Factory.create(:tag, :name => "bbb")
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
ta = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
normalized_tags = TagAlias.to_aliased(["aaa", "ccc"])
assert_equal(["bbb", "ccc"], normalized_tags.sort)
end
@@ -30,7 +30,7 @@ class TagAliasTest < ActiveSupport::TestCase
should "update the cache" do
tag1 = Factory.create(:tag, :name => "aaa")
tag2 = Factory.create(:tag, :name => "bbb")
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
ta = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_equal("bbb", MEMCACHE.get("ta:aaa"))
ta.destroy
assert_nil(MEMCACHE.get("ta:aaa"))
@@ -42,7 +42,7 @@ class TagAliasTest < ActiveSupport::TestCase
post2 = Factory.create(:post, :tag_string => "ccc ddd")
assert_equal("aaa bbb", post1.tag_string)
assert_equal("ccc ddd", post2.tag_string)
ta = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "ccc")
ta = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "ccc")
post1.reload
post2.reload
assert_equal("ccc bbb", post1.tag_string)
@@ -50,7 +50,7 @@ class TagAliasTest < ActiveSupport::TestCase
end
should "not validate for transitive relations" do
ta1 = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
ta1 = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_difference("TagAlias.count", 0) do
ta3 = Factory.build(:tag_alias, :antecedent_name => "bbb", :consequent_name => "ddd")
ta3.save
@@ -60,15 +60,15 @@ class TagAliasTest < ActiveSupport::TestCase
end
should "record the alias's creator in the tag history" do
user = Factory.create(:user)
p1 = nil
CurrentUser.scoped(user, "127.0.0.1") do
p1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
uploader = Factory.create(:user)
post = nil
CurrentUser.scoped(uploader, "127.0.0.1") do
post = Factory.create(:post, :tag_string => "aaa bbb ccc")
end
ta1 = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "xxx")
p1.reload
assert_not_equal(ta1.creator_id, p1.uploader_id)
assert_equal(ta1.creator_id, p1.versions.last.updater_id)
tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "xxx")
post.reload
assert_not_equal(tag_alias.creator_id, post.uploader_id)
assert_equal(tag_alias.creator_id, post.versions.last.updater_id)
end
end
end

View File

@@ -16,12 +16,12 @@ class TagImplicationTest < ActiveSupport::TestCase
end
should "populate the creator information" do
ti = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_equal(CurrentUser.user.id, ti.creator_id)
end
should "not validate when a circular relation is created" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = Factory.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa")
ti2.save
assert(ti2.errors.any?, "Tag implication should not have validated.")
@@ -29,34 +29,18 @@ class TagImplicationTest < ActiveSupport::TestCase
end
should "not allow for duplicates" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = Factory.build(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2.save
assert(ti2.errors.any?, "Tag implication should not have validated.")
assert_equal("Antecedent name has already been taken", ti2.errors.full_messages.join(""))
end
should "clear the cache upon saving" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_equal(["bbb"], ti1.descendant_names_array)
assert_equal(["bbb"], MEMCACHE.get("ti:aaa"))
ti1.update_attributes(
:consequent_name => "ccc"
)
assert_equal(["ccc"], MEMCACHE.get("ti:aaa"))
end
should "clear the cache upon destruction" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti1.destroy
assert_nil(MEMCACHE.get("ti:aaa"))
end
should "calculate all its descendants" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc")
assert_equal("ccc", ti1.descendant_names)
assert_equal(["ccc"], ti1.descendant_names_array)
ti2 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_equal("bbb ccc", ti2.descendant_names)
assert_equal(["bbb", "ccc"], ti2.descendant_names_array)
ti1.reload
@@ -64,15 +48,9 @@ class TagImplicationTest < ActiveSupport::TestCase
assert_equal(["ccc"], ti1.descendant_names_array)
end
should "cache its descendants" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
assert_equal(["bbb"], ti1.descendant_names_array)
assert_equal(["bbb"], MEMCACHE.get("ti:aaa"))
end
should "update its descendants on save" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = Factory.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = FactoryGirl.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd")
ti2.update_attributes(
:antecedent_name => "bbb"
)
@@ -83,10 +61,10 @@ class TagImplicationTest < ActiveSupport::TestCase
end
should "update the decendants for its parent on save" do
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = Factory.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc")
ti3 = Factory.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd")
ti4 = Factory.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "eee")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = FactoryGirl.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc")
ti3 = FactoryGirl.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd")
ti4 = FactoryGirl.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "eee")
ti1.reload
ti2.reload
ti3.reload
@@ -99,8 +77,8 @@ class TagImplicationTest < ActiveSupport::TestCase
should "update any affected post upon save" do
p1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx")
ti2 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "yyy")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx")
ti2 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "yyy")
p1.reload
assert_equal("aaa yyy xxx bbb ccc", p1.tag_string)
end
@@ -111,7 +89,7 @@ class TagImplicationTest < ActiveSupport::TestCase
CurrentUser.scoped(user, "127.0.0.1") do
p1 = Factory.create(:post, :tag_string => "aaa bbb ccc")
end
ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx")
ti1 = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx")
p1.reload
assert_not_equal(ti1.creator_id, p1.uploader_id)
assert_equal(ti1.creator_id, p1.versions.last.updater_id)