OSDN Git Service

img.azyobuzi.net エラー時のフォールバック処理を改善
authorKimura Youichi <kim.upsilon@bucyou.net>
Mon, 21 Jan 2013 05:09:17 +0000 (14:09 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 22 Jan 2013 07:10:14 +0000 (16:10 +0900)
OpenTween.Tests/Thumbnail/Services/ImgAzyobuziNetTest.cs
OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs

index a7a6485..cf69b86 100644 (file)
@@ -55,6 +55,12 @@ namespace OpenTween.Thumbnail.Services
                 if (apiBase == "http://down.example.com/api/")
                     throw new WebException();
 
+                if (apiBase == "http://error.example.com/api/")
+                    return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
+
+                if (apiBase == "http://invalid.example.com/api/")
+                    return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
+
                 return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
             }
         }
@@ -62,10 +68,37 @@ namespace OpenTween.Thumbnail.Services
         [Test]
         public void HostFallbackTest()
         {
-            var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
+            var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
+            service.LoadRegex();
+            Assert.That(service.GetApiBase(), Is.EqualTo("http://avail1.example.com/api/"));
+
+            service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
+            service.LoadRegex();
+            Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
 
+            service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
             service.LoadRegex();
             Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
+
+            service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
+            service.LoadRegex();
+            Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
+
+            service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
+            service.LoadRegex();
+            Assert.That(service.GetApiBase(), Is.Null);
+        }
+
+        [Test]
+        public void ServerOutageTest()
+        {
+            var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
+
+            service.LoadRegex();
+            Assert.That(service.GetApiBase(), Is.Null);
+
+            var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
+            Assert.That(thumbinfo, Is.Null);
         }
 
         [Test]
index c3dc991..b793574 100644 (file)
@@ -40,7 +40,7 @@ namespace OpenTween.Thumbnail.Services
         };
 
         protected string ApiBase;
-        protected IEnumerable<Regex> UrlRegex = new Regex[] {};
+        protected IEnumerable<Regex> UrlRegex = null;
         protected Timer UpdateTimer;
 
         private object LockObj = new object();
@@ -92,6 +92,13 @@ namespace OpenTween.Thumbnail.Services
 #endif
                 }
             }
+
+            // どのサーバーも使用できない場合
+            lock (this.LockObj)
+            {
+                this.UrlRegex = null;
+                this.ApiBase = null;
+            }
         }
 
         public bool LoadRegex(string apiBase)
@@ -102,6 +109,9 @@ namespace OpenTween.Thumbnail.Services
                 {
                     var xElm = XElement.Load(jsonReader);
 
+                    if (xElm.Element("error") != null)
+                        return false;
+
                     lock (this.LockObj)
                     {
                         this.UrlRegex = xElm.Elements("item")
@@ -114,7 +124,8 @@ namespace OpenTween.Thumbnail.Services
 
                 return true;
             }
-            catch (WebException) { }
+            catch (WebException) { } // サーバーが2xx以外のステータスコードを返した場合
+            catch (XmlException) { } // サーバーが不正なJSONを返した場合
 
             return false;
         }
@@ -131,6 +142,9 @@ namespace OpenTween.Thumbnail.Services
         {
             lock (this.LockObj)
             {
+                if (this.UrlRegex == null)
+                    return null;
+
                 foreach (var regex in this.UrlRegex)
                 {
                     if (regex.IsMatch(url))