Skip to content

Commit 34bbf81

Browse files
author
Amir Tocker
committed
Add Timeout attribute to the API call.
1 parent 1cf2fce commit 34bbf81

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

Cloudinary.Test/CloudinaryTest.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using NUnit.Framework;
44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Drawing;
78
using System.IO;
89
using System.Linq;
@@ -72,7 +73,6 @@ public void Initialize()
7273
Assert.IsFalse(String.IsNullOrEmpty(m_account.ApiSecret));
7374

7475
m_cloudinary = new Cloudinary(m_account);
75-
7676
if (!String.IsNullOrWhiteSpace(Settings.Default.ApiBaseAddress))
7777
m_cloudinary.Api.ApiBaseAddress = Settings.Default.ApiBaseAddress;
7878

@@ -115,6 +115,44 @@ public void TestUploadLocalImage()
115115
Assert.AreEqual(expectedSign, uploadResult.Signature);
116116
}
117117

118+
[Test]
119+
public void TestUploadLocalImageTimeout()
120+
{
121+
var code = WebExceptionStatus.Success;
122+
var timeout = 11000;
123+
var uploadParams = new ImageUploadParams()
124+
{
125+
File = new FileDescription(m_testImagePath)
126+
};
127+
var origAddr = m_cloudinary.Api.ApiBaseAddress;
128+
Stopwatch stopWatch = new Stopwatch();
129+
m_cloudinary.Api.ApiBaseAddress = "https://10.255.255.1";
130+
m_cloudinary.Api.Timeout = timeout;
131+
try
132+
{
133+
stopWatch.Start();
134+
m_cloudinary.Upload(uploadParams);
135+
}
136+
catch (WebException e)
137+
{
138+
Console.WriteLine("Error {0}", e.Message);
139+
code = e.Status;
140+
stopWatch.Stop();
141+
142+
}
143+
finally
144+
{
145+
m_cloudinary.Api.ApiBaseAddress = origAddr;
146+
stopWatch.Stop();
147+
}
148+
149+
//Assert.AreEqual(WebExceptionStatus.Timeout, code);
150+
Console.WriteLine("Elapsed {0}", stopWatch.ElapsedMilliseconds);
151+
Assert.LessOrEqual(timeout - 1000, stopWatch.ElapsedMilliseconds);
152+
Assert.GreaterOrEqual(timeout + 1000, stopWatch.ElapsedMilliseconds);
153+
154+
}
155+
118156
[Test]
119157
public void TestUploadLocalVideo()
120158
{

Cloudinary.Test/app.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<startup/>
99
<applicationSettings>
1010
<Cloudinary.Test.Properties.Settings>
11+
<setting name="ApiBaseAddress" serializeAs="String">
12+
<value />
13+
</setting>
1114
<setting name="CloudName" serializeAs="String">
1215
<value></value>
1316
</setting>
@@ -17,9 +20,6 @@
1720
<setting name="ApiSecret" serializeAs="String">
1821
<value></value>
1922
</setting>
20-
<setting name="ApiBaseAddress" serializeAs="String">
21-
<value />
22-
</setting>
2323
</Cloudinary.Test.Properties.Settings>
2424
</applicationSettings>
2525
</configuration>

Cloudinary.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Express 2012 for Web
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.24720.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloudinary", "Cloudinary\Cloudinary.csproj", "{6C0ACD6C-58BC-4571-A59D-EB752317C8A2}"
57
EndProject
68
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloudinary.Test", "Cloudinary.Test\Cloudinary.Test.csproj", "{16C07542-4726-4AE7-9FDD-62DB823B449E}"
@@ -12,6 +14,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{D5DD0E
1214
.nuget\NuGet.targets = .nuget\NuGet.targets
1315
EndProjectSection
1416
EndProject
17+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DB3C0EFC-024A-4850-B201-375BC198F376}"
18+
ProjectSection(SolutionItems) = preProject
19+
sdk-test.testsettings = sdk-test.testsettings
20+
EndProjectSection
21+
EndProject
1522
Global
1623
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1724
Debug|Any CPU = Debug|Any CPU

Cloudinary/Api.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class Api : ISignProvider
3434
public string PrivateCdn;
3535
public string Suffix;
3636

37+
public int Timeout = 0;
38+
3739
/// <summary>
3840
/// Sets whether to use the use chunked encoding. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 for further info.
3941
/// Server must support HTTP/1.1 in order to use the chunked encoding.
@@ -304,10 +306,14 @@ public HttpWebResponse Call(HttpMethod method, string url, SortedDictionary<stri
304306
Console.WriteLine(String.Format("{0} REQUEST:", method));
305307
Console.WriteLine(url);
306308
#endif
307-
309+
308310
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
309311
request.Method = Enum.GetName(typeof(HttpMethod), method);
310312
request.UserAgent = USER_AGENT;
313+
if (Timeout > 0)
314+
{
315+
request.Timeout = Timeout;
316+
}
311317

312318
if (method == HttpMethod.POST && parameters != null)
313319
{

Cloudinary/Cloudinary.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<Compile Include="Actions\RawUploadParams.cs" />
118118
<Compile Include="Actions\ImageUploadResult.cs" />
119119
<Compile Include="Properties\AssemblyInfo.cs" />
120+
<Compile Include="Properties\Settings.Designer.cs">
121+
<AutoGen>True</AutoGen>
122+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
123+
<DependentUpon>Settings.settings</DependentUpon>
124+
</Compile>
120125
<Compile Include="StringDictionary.cs" />
121126
<Compile Include="Transforms\ImageTransform.cs" />
122127
<Compile Include="Transforms\VideoTransform.cs" />

Cloudinary/Properties/Settings.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)