• R/O
  • HTTP
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

dev


コミットメタ情報

リビジョン8969c087848b728ef97d4ed077c22986e0c1f1b6 (tree)
日時2013-01-22 16:10:14
作者Kimura Youichi <kim.upsilon@bucy...>
コミッターKimura Youichi

ログメッセージ

img.azyobuzi.net エラー時のフォールバック処理を改善

変更サマリ

差分

--- a/OpenTween.Tests/Thumbnail/Services/ImgAzyobuziNetTest.cs
+++ b/OpenTween.Tests/Thumbnail/Services/ImgAzyobuziNetTest.cs
@@ -55,6 +55,12 @@ namespace OpenTween.Thumbnail.Services
5555 if (apiBase == "http://down.example.com/api/")
5656 throw new WebException();
5757
58+ if (apiBase == "http://error.example.com/api/")
59+ return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
60+
61+ if (apiBase == "http://invalid.example.com/api/")
62+ return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
63+
5864 return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
5965 }
6066 }
@@ -62,10 +68,37 @@ namespace OpenTween.Thumbnail.Services
6268 [Test]
6369 public void HostFallbackTest()
6470 {
65- var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
71+ var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
72+ service.LoadRegex();
73+ Assert.That(service.GetApiBase(), Is.EqualTo("http://avail1.example.com/api/"));
74+
75+ service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
76+ service.LoadRegex();
77+ Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
6678
79+ service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
6780 service.LoadRegex();
6881 Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
82+
83+ service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
84+ service.LoadRegex();
85+ Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
86+
87+ service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
88+ service.LoadRegex();
89+ Assert.That(service.GetApiBase(), Is.Null);
90+ }
91+
92+ [Test]
93+ public void ServerOutageTest()
94+ {
95+ var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
96+
97+ service.LoadRegex();
98+ Assert.That(service.GetApiBase(), Is.Null);
99+
100+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
101+ Assert.That(thumbinfo, Is.Null);
69102 }
70103
71104 [Test]
--- a/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
+++ b/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
@@ -40,7 +40,7 @@ namespace OpenTween.Thumbnail.Services
4040 };
4141
4242 protected string ApiBase;
43- protected IEnumerable<Regex> UrlRegex = new Regex[] {};
43+ protected IEnumerable<Regex> UrlRegex = null;
4444 protected Timer UpdateTimer;
4545
4646 private object LockObj = new object();
@@ -92,6 +92,13 @@ namespace OpenTween.Thumbnail.Services
9292 #endif
9393 }
9494 }
95+
96+ // どのサーバーも使用できない場合
97+ lock (this.LockObj)
98+ {
99+ this.UrlRegex = null;
100+ this.ApiBase = null;
101+ }
95102 }
96103
97104 public bool LoadRegex(string apiBase)
@@ -102,6 +109,9 @@ namespace OpenTween.Thumbnail.Services
102109 {
103110 var xElm = XElement.Load(jsonReader);
104111
112+ if (xElm.Element("error") != null)
113+ return false;
114+
105115 lock (this.LockObj)
106116 {
107117 this.UrlRegex = xElm.Elements("item")
@@ -114,7 +124,8 @@ namespace OpenTween.Thumbnail.Services
114124
115125 return true;
116126 }
117- catch (WebException) { }
127+ catch (WebException) { } // サーバーが2xx以外のステータスコードを返した場合
128+ catch (XmlException) { } // サーバーが不正なJSONを返した場合
118129
119130 return false;
120131 }
@@ -131,6 +142,9 @@ namespace OpenTween.Thumbnail.Services
131142 {
132143 lock (this.LockObj)
133144 {
145+ if (this.UrlRegex == null)
146+ return null;
147+
134148 foreach (var regex in this.UrlRegex)
135149 {
136150 if (regex.IsMatch(url))