Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

ファイルをDL/UL時にはタイムアウトを10秒でなく120秒にする #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ncmb_unity/Assets/NCMB/Script/NCMBConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class NCMBConnection
private static readonly string HEADER_USER_AGENT_VALUE = "unity-" + CommonConstant.SDK_VERSION;

// time out 10 sec
private static readonly int REQUEST_TIME_OUT = 10;
// private static readonly int REQUEST_TIME_OUT = 10;

private string _applicationKey = "";
private string _clientKey = "";
Expand All @@ -86,6 +86,8 @@ public class NCMBConnection
private Uri _domainUri = null;
private NCMBFile _file = null;
internal UnityWebRequest _request = null;
// request time out
private int _requestTimeout = 10;


//コンストラクタ(通常)
Expand All @@ -101,7 +103,7 @@ internal NCMBConnection (String url, ConnectType method, string content, string
}

//コンストラクタ
internal NCMBConnection (String url, ConnectType method, string content, string sessionToken, NCMBFile file, string domain)
internal NCMBConnection (String url, ConnectType method, string content, string sessionToken, NCMBFile file, string domain, int requestTimeout = 10)
{
this._method = method;
this._content = content;
Expand All @@ -112,6 +114,7 @@ internal NCMBConnection (String url, ConnectType method, string content, string
this._domainUri = new Uri (domain);
this._file = file;
this._request = _returnRequest ();
this._requestTimeout = requestTimeout;

}

Expand Down Expand Up @@ -399,7 +402,7 @@ internal static IEnumerator SendRequest (NCMBConnection connection, UnityWebRequ
while (!req.isDone) {
//elapsedTime += Time.deltaTime;
elapsedTime += waitTime;
if (elapsedTime >= REQUEST_TIME_OUT) {
if (elapsedTime >= connection._requestTimeout) {
req.Abort ();
error = new NCMBException ();
break;
Expand Down
7 changes: 5 additions & 2 deletions ncmb_unity/Assets/NCMB/Script/NCMBFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace NCMB
public class NCMBFile : NCMBObject
{

// Time out 120 sec
private static readonly int FILE_REQUEST_TIME_OUT = 120;

/// <summary>
/// ファイル名の取得、または設定を行います。
/// </summary>
Expand Down Expand Up @@ -100,7 +103,7 @@ public override void SaveAsync (NCMBCallback callback)
IDictionary<string, INCMBFieldOperation> currentOperations = null;
currentOperations = this.StartSave ();
string content = _toJSONObjectForSaving (currentOperations);
NCMBConnection con = new NCMBConnection (_getBaseUrl (), type, content, NCMBUser._getCurrentSessionToken (), this);
NCMBConnection con = new NCMBConnection (_getBaseUrl (), type, content, NCMBUser._getCurrentSessionToken (), this, NCMBSettings.DomainURL, FILE_REQUEST_TIME_OUT);
con.Connect (delegate(int statusCode, string responseData, NCMBException error) {
try {
NCMBDebug.Log ("【StatusCode】:" + statusCode + Environment.NewLine + "【Error】:" + error + Environment.NewLine + "【ResponseData】:" + responseData);
Expand Down Expand Up @@ -145,7 +148,7 @@ public void FetchAsync (NCMBGetFileCallback callback)
}

// 通信処理
NCMBConnection con = new NCMBConnection (_getBaseUrl (), ConnectType.GET, null, NCMBUser._getCurrentSessionToken (), this);
NCMBConnection con = new NCMBConnection (_getBaseUrl (), ConnectType.GET, null, NCMBUser._getCurrentSessionToken (), this, NCMBSettings.DomainURL, FILE_REQUEST_TIME_OUT);
con.Connect (delegate(int statusCode, byte[] responseData, NCMBException error) {
NCMBDebug.Log ("【StatusCode】:" + statusCode + Environment.NewLine + "【Error】:" + error + Environment.NewLine + "【ResponseData】:" + responseData);
this.estimatedData ["fileData"] = responseData;
Expand Down
14 changes: 13 additions & 1 deletion ncmb_unity/Assets/PlayModeTest/MockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using NCMB.Internal;
using System.Security.Cryptography;
using System.Threading;

public class MockServer
{
Expand All @@ -32,7 +33,7 @@ public class MockServer
private static string DATA_PATH_DEFAULT = "PlayModeTest/mbaas.yaml";
//Dictionary to store all mock data
Dictionary<string, List<MockServerObject>> mockObjectDic = new Dictionary<string, List<MockServerObject>> ();
Uri _domainUri = new Uri(NCMBTestSettings.DOMAIN_URL);
Uri _domainUri = new Uri(NCMBTestSettings.DOMAIN_URL);

public MockServer()
{
Expand Down Expand Up @@ -142,6 +143,11 @@ private void checkAndResponse (HttpListenerRequest request, HttpListenerResponse
string signature = _makeResponseSignature(request, mockObj.responseJson);
response.AddHeader("X-NCMB-Response-Signature", signature);
}

if(mockObj.delay > 0){
Thread.Sleep(mockObj.delay);
}

//Set status code
response.StatusCode = mockObj.status;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes (mockObj.responseJson);
Expand Down Expand Up @@ -219,6 +225,12 @@ private void ReadMockData ()
mock.responseSignature = true;
}

if (response.Children.Keys.Contains(new YamlScalarNode("delay")))
{
YamlScalarNode delay = (YamlScalarNode)response.Children [new YamlScalarNode ("delay")];
mock.delay = Convert.ToInt32 (delay.Value);;
}

YamlScalarNode status = (YamlScalarNode)response.Children [new YamlScalarNode ("status")];
mock.status = Convert.ToInt32 (status.Value);

Expand Down
1 change: 1 addition & 0 deletions ncmb_unity/Assets/PlayModeTest/MockServerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class MockServerObject
public string responseJson = "";
public int status = 404;
public bool responseSignature = false;
public int delay = 0;

public void validate()
{
Expand Down
21 changes: 21 additions & 0 deletions ncmb_unity/Assets/PlayModeTest/NCMBObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,26 @@ public void GetBaseUrlTest ()
}


/**
* - 内容:タイムアウトのエラーをチェックする
* - 結果:保存の時にタイムアウトした。(error 408)
*/
[UnityTest]
public IEnumerator SaveObjectTimeout ()
{
// テストデータ作成
NCMBObject obj = new NCMBObject ("TestClass");
obj ["key"] = "\"timeout-test\"";
obj.SaveAsync ((NCMBException e) => {
if (e != null && string.Compare("408", e.ErrorCode) == 0) {
Assert.True(true);

} else {
Assert.True (false);
}
NCMBTestSettings.CallbackFlag = true;
});
yield return NCMBTestSettings.AwaitAsync ();
}

}
12 changes: 12 additions & 0 deletions ncmb_unity/Assets/PlayModeTest/mbaas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ response:
status: 201
file: /json/post_success_response.json
---
request:
url: 2013-09-01/classes/TestClass
method: POST
body:
key: \"timeout-test\"
response:
status: 201
file: /json/post_success_response.json
delay: 12000
---
request:
url: 2013-09-01/classes/TestClass
method: GET
Expand Down Expand Up @@ -428,13 +438,15 @@ request:
response:
status: 200
file: /json/create_date_response.json
delay: 12000
---
request:
url: 2013-09-01/files/test.txt
method: GET
response:
status: 200
file: /json/file_hello_response.json
delay: 12000
---
request:
url: 2013-09-01/users
Expand Down