Skip to content

Commit

Permalink
* 增加接口单测case,提升覆盖率
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnnqin committed Mar 15, 2023
1 parent db12e05 commit a4d2773
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
76 changes: 72 additions & 4 deletions QCloudCSharpSDK/COSXMLTests/ObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public class ObjectTest

internal string copykey;

internal string bigCopyKey;

internal string copySourceFilePath;

internal string bigCopySourceFilePath;

internal string copyKeySmall;

Expand Down Expand Up @@ -75,8 +79,11 @@ public void Init()
multiKey = "bigObject" + currentTime;
copykey = "copy_objecttest.txt";
copySourceFilePath = QCloudServer.CreateFile(copykey, 1024 * 1024 * 1);
bigCopyKey = "copy_objecttext_big.txt";
bigCopySourceFilePath = QCloudServer.CreateFile(bigCopyKey, 10 * 1024 * 1024);
copyKeySmall = "copy_target";
selectKey = "select_target.json";
selectFilePath = QCloudServer.CreateJsonFile(selectKey);
appendKey = "appendableObject";

CreateBucketsIfNotExist(bucket);
Expand Down Expand Up @@ -278,7 +285,7 @@ public void PutObject()
Assert.NotNull(result.eTag);
Assert.True(COSXML.Utils.Crc64.CompareCrc64(smallFileSrcPath, result.crc64ecma));

//Put Copy测试的Source Object
//Put Copy 测试的 Source Object
request = new PutObjectRequest(bucket, copykey, copySourceFilePath);
result = cosXml.PutObject(request);

Expand All @@ -287,6 +294,27 @@ public void PutObject()
Assert.NotNull(result.eTag);
Assert.True(COSXML.Utils.Crc64.CompareCrc64(copySourceFilePath, result.crc64ecma));

//Put Copy 测试的大 Source Object
request = new PutObjectRequest(bucket, bigCopyKey, bigCopySourceFilePath);
result = cosXml.PutObject(request);

Console.WriteLine(result.GetResultInfo());
Assert.AreEqual(200, result.httpCode);
Assert.NotNull(result.eTag);
Assert.True(COSXML.Utils.Crc64.CompareCrc64(bigCopySourceFilePath, result.crc64ecma));

request = new PutObjectRequest(bucket, copyKeySmall, copySourceFilePath);
result = cosXml.PutObject(request);

//Select 测试的 json 文件
request = new PutObjectRequest(bucket, selectKey, selectFilePath);
result = cosXml.PutObject(request);

Console.WriteLine(result.GetResultInfo());
Assert.AreEqual(200, result.httpCode);
Assert.NotNull(result.eTag);
Assert.True(COSXML.Utils.Crc64.CompareCrc64(selectFilePath, result.crc64ecma));

request = new PutObjectRequest(bucket, copyKeySmall, copySourceFilePath);
result = cosXml.PutObject(request);

Expand Down Expand Up @@ -1488,14 +1516,55 @@ public void TestDownloadResumableTask()
}

Thread.Sleep(500);
}
}
else
{
Console.WriteLine("localFileCrc64 = " + downloadTask.GetLocalFileCrc64());
Thread.Sleep(500);
}
}

[Test()]
public void TestDownloadPauseAndRetry()
{
//执行请求
GetObjectRequest getRequest = new GetObjectRequest(bucket, multiKey, localDir, localFileName);
getRequest.LimitTraffic(8 * 1024 * 1024);

COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(getRequest);
downloadTask.SetResumableDownload(true);

double progrss = 0;
downloadTask.progressCallback = delegate (long completed, long total)
{
progrss = completed * 100.0 / total;
// Console.WriteLine(String.Format("progress = {0:##.##}%", progrss));
};

var asyncTask = transferManager.DownloadAsync(downloadTask);

asyncTask = downloadTask.AsyncTask<COSXMLDownloadTask.DownloadTaskResult>();

if (downloadTask.State() != TaskState.Completed && downloadTask.State() != TaskState.Failed)
{
Console.WriteLine("downloadTask.State() = " + downloadTask.State());
Console.WriteLine("localFileCrc64 = " + downloadTask.GetLocalFileCrc64());
if (60 < progrss && progrss < 65)
{
downloadTask.Pause();
}

Thread.Sleep(500);
}

getRequest = new GetObjectRequest(bucket, multiKey, localDir, localFileName);
getRequest.LimitTraffic(8 * 1024 * 1024);

downloadTask = new COSXMLDownloadTask(getRequest);
downloadTask.SetResumableDownload(true);
transferManager.Download(downloadTask);
}

[Test()]
public void TestDownloadTaskCancelled()
{
Expand Down Expand Up @@ -1558,7 +1627,7 @@ public void TestDownloadTaskOverwriteSameFile()
public async Task TestCopyTaskWithBigFile()
{
CopySourceStruct copySource = new CopySourceStruct(QCloudServer.Instance().appid,
bucket, QCloudServer.Instance().region, copykey);
bucket, QCloudServer.Instance().region, bigCopyKey);


COSXMLCopyTask copyTask = new COSXMLCopyTask(bucket, multiKey, copySource);
Expand All @@ -1582,7 +1651,6 @@ public async Task TestCopyTaskWithSmallFile()
bucket, QCloudServer.Instance().region, copyKeySmall);

COSXMLCopyTask copyTask = new COSXMLCopyTask(bucket, multiKey, copySource);

COSXMLCopyTask.CopyTaskResult result = await transferManager.CopyAsync(copyTask);

Assert.True(result.httpCode == 200);
Expand Down
21 changes: 21 additions & 0 deletions QCloudCSharpSDK/COSXMLTests/QCloudServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ public static string CreateFile(string filename, long size)
}
}

public static string CreateJsonFile(string filename)
{

try
{
string path = null;
FileStream fs = new FileStream(filename, FileMode.Create);
string s = "{\"Statement\":[{\"Principal\":{\"service\":\"cos.qcloud.com\"},\"Effect\":\"Allow\",\"Action\":[\"name/cos:PutObject\",\"name/cos:HeadObject\",\"name/cos:InitiateMultipartUpload\",\"name/cos:UploadPart\",\"name/cos:CompleteMultipartUpload\"],\"Resource\":[\"qcs::cos:<region>:uid/<appid>:<bucketname-appid>/*\"]}],\"version\":\"2.0\"}";
byte[] buffer = Encoding.Default.GetBytes(s);
fs.Write(buffer, 0, buffer.Length);
path = fs.Name;
fs.Close();

return path;
}
catch (Exception)
{
throw;
}
}

public static void SetRequestACLData(CosRequest request)
{
request.GetType().GetMethod("SetCosACL", new[] { typeof(CosACL) }).Invoke(request, new object[] { CosACL.Private });
Expand Down

0 comments on commit a4d2773

Please sign in to comment.