dev
リビジョン | 8969c087848b728ef97d4ed077c22986e0c1f1b6 (tree) |
---|---|
日時 | 2013-01-22 16:10:14 |
作者 | Kimura Youichi <kim.upsilon@bucy...> |
コミッター | Kimura Youichi |
img.azyobuzi.net エラー時のフォールバック処理を改善
@@ -55,6 +55,12 @@ namespace OpenTween.Thumbnail.Services | ||
55 | 55 | if (apiBase == "http://down.example.com/api/") |
56 | 56 | throw new WebException(); |
57 | 57 | |
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 | + | |
58 | 64 | return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]"); |
59 | 65 | } |
60 | 66 | } |
@@ -62,10 +68,37 @@ namespace OpenTween.Thumbnail.Services | ||
62 | 68 | [Test] |
63 | 69 | public void HostFallbackTest() |
64 | 70 | { |
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/")); | |
66 | 78 | |
79 | + service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" }); | |
67 | 80 | service.LoadRegex(); |
68 | 81 | 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); | |
69 | 102 | } |
70 | 103 | |
71 | 104 | [Test] |
@@ -40,7 +40,7 @@ namespace OpenTween.Thumbnail.Services | ||
40 | 40 | }; |
41 | 41 | |
42 | 42 | protected string ApiBase; |
43 | - protected IEnumerable<Regex> UrlRegex = new Regex[] {}; | |
43 | + protected IEnumerable<Regex> UrlRegex = null; | |
44 | 44 | protected Timer UpdateTimer; |
45 | 45 | |
46 | 46 | private object LockObj = new object(); |
@@ -92,6 +92,13 @@ namespace OpenTween.Thumbnail.Services | ||
92 | 92 | #endif |
93 | 93 | } |
94 | 94 | } |
95 | + | |
96 | + // どのサーバーも使用できない場合 | |
97 | + lock (this.LockObj) | |
98 | + { | |
99 | + this.UrlRegex = null; | |
100 | + this.ApiBase = null; | |
101 | + } | |
95 | 102 | } |
96 | 103 | |
97 | 104 | public bool LoadRegex(string apiBase) |
@@ -102,6 +109,9 @@ namespace OpenTween.Thumbnail.Services | ||
102 | 109 | { |
103 | 110 | var xElm = XElement.Load(jsonReader); |
104 | 111 | |
112 | + if (xElm.Element("error") != null) | |
113 | + return false; | |
114 | + | |
105 | 115 | lock (this.LockObj) |
106 | 116 | { |
107 | 117 | this.UrlRegex = xElm.Elements("item") |
@@ -114,7 +124,8 @@ namespace OpenTween.Thumbnail.Services | ||
114 | 124 | |
115 | 125 | return true; |
116 | 126 | } |
117 | - catch (WebException) { } | |
127 | + catch (WebException) { } // サーバーが2xx以外のステータスコードを返した場合 | |
128 | + catch (XmlException) { } // サーバーが不正なJSONを返した場合 | |
118 | 129 | |
119 | 130 | return false; |
120 | 131 | } |
@@ -131,6 +142,9 @@ namespace OpenTween.Thumbnail.Services | ||
131 | 142 | { |
132 | 143 | lock (this.LockObj) |
133 | 144 | { |
145 | + if (this.UrlRegex == null) | |
146 | + return null; | |
147 | + | |
134 | 148 | foreach (var regex in this.UrlRegex) |
135 | 149 | { |
136 | 150 | if (regex.IsMatch(url)) |