Skip to content

Commit

Permalink
fix for openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
普冬 committed Nov 15, 2024
1 parent 8e25a7e commit 840a88a
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 43 deletions.
50 changes: 25 additions & 25 deletions DaraUnitTests/DaraCoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(0L, DaraCore.GetBackoffDelay(null, retryPolicyContext));
Assert.Equal(0, DaraCore.GetBackoffDelay(null, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -493,7 +493,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(100L, DaraCore.GetBackoffDelay(null, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(null, retryPolicyContext));

RetryOptions retryOptions = new RetryOptions
{
Expand All @@ -502,7 +502,7 @@ public void TestGetBackoffDelay()
NoRetryCondition = null
};

Assert.Equal(100L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryOptions = new RetryOptions
{
Expand All @@ -511,7 +511,7 @@ public void TestGetBackoffDelay()
NoRetryCondition = new List<RetryCondition> { retryCondition3 }
};

Assert.Equal(400L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(400, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -523,7 +523,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(800L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(800, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -535,7 +535,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(400L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(400, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));


retryPolicyContext = new RetryPolicyContext
Expand All @@ -548,7 +548,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(400L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(400, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -559,7 +559,7 @@ public void TestGetBackoffDelay()
Code = "CExceptionCode"
}
};
Assert.Equal(800L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(800, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -570,7 +570,7 @@ public void TestGetBackoffDelay()
Code = "CExceptionCode"
}
};
Assert.Equal(1600L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(1600, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

RetryCondition retryCondition4 = new RetryCondition
{
Expand All @@ -593,7 +593,7 @@ public void TestGetBackoffDelay()
Code = "AExceptionCode"
}
};
Assert.Equal(60000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(60000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

backoffPolicy = new ExponentialBackoffPolicy(200, 180 * 1000);

Expand All @@ -620,7 +620,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(120000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(120000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));


retryPolicyContext = new RetryPolicyContext
Expand All @@ -632,7 +632,7 @@ public void TestGetBackoffDelay()
Code = "AExceptionCode"
}
};
Assert.Equal(120000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(120000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryCondition4 = new RetryCondition
{
Expand All @@ -658,7 +658,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(30000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(30000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -670,7 +670,7 @@ public void TestGetBackoffDelay()
}
};

Assert.Equal(30000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(30000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryCondition4 = new RetryCondition
{
Expand All @@ -694,7 +694,7 @@ public void TestGetBackoffDelay()
Code = "AExceptionCode"
}
};
Assert.Equal(100L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
}


Expand All @@ -721,23 +721,23 @@ public void TestThrottlingBackoffDelay()
RetriesAttempted = 1,
Exception = new ThrottlingException { }
};
Assert.Equal(100L, DaraCore.GetBackoffDelay(null, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(null, retryPolicyContext));

RetryOptions retryOptions = new RetryOptions
{
Retryable = false,
RetryCondition = new List<RetryCondition> { retryCondition },
NoRetryCondition = null
};
Assert.Equal(100L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryOptions = new RetryOptions
{
Retryable = true,
RetryCondition = new List<RetryCondition> { retryCondition },
NoRetryCondition = null
};
Assert.Equal(100L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -746,7 +746,7 @@ public void TestThrottlingBackoffDelay()
Message = "ThrottlingException"
}
};
Assert.Equal(400L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(400, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -757,7 +757,7 @@ public void TestThrottlingBackoffDelay()
RetryAfter = 2000L
}
};
Assert.Equal(2000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(2000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -768,7 +768,7 @@ public void TestThrottlingBackoffDelay()
RetryAfter = 320 * 1000L
}
};
Assert.Equal(120000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(120000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryCondition = new RetryCondition
{
Expand All @@ -782,7 +782,7 @@ public void TestThrottlingBackoffDelay()
RetryCondition = new List<RetryCondition> { retryCondition },
NoRetryCondition = null
};
Assert.Equal(100L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(100, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -793,7 +793,7 @@ public void TestThrottlingBackoffDelay()
RetryAfter = 2000L
}
};
Assert.Equal(2000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(2000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -804,7 +804,7 @@ public void TestThrottlingBackoffDelay()
RetryAfter = 2000L
}
};
Assert.Equal(2000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(2000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));

retryPolicyContext = new RetryPolicyContext
{
Expand All @@ -815,7 +815,7 @@ public void TestThrottlingBackoffDelay()
RetryAfter = 2000L
}
};
Assert.Equal(2000L, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
Assert.Equal(2000, DaraCore.GetBackoffDelay(retryOptions, retryPolicyContext));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion DaraUnitTests/DaraUnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AssemblyName>DaraUnitTests</AssemblyName>
Expand Down
24 changes: 18 additions & 6 deletions Darabonba/DaraCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ public static int GetBackoffTime(IDictionary Idict, int retryTimes)
return backOffTime;
}

public static long GetBackoffDelay(RetryOptions options, RetryPolicyContext ctx)
public static int GetBackoffDelay(RetryOptions options, RetryPolicyContext ctx)
{
if (ctx.RetriesAttempted == null || ctx.RetriesAttempted == 0)
{
return 0L;
return 0;
}

if (ctx.Exception is DaraException)
Expand All @@ -305,21 +305,21 @@ public static long GetBackoffDelay(RetryOptions options, RetryPolicyContext ctx)
var ResponseException = (ResponseException)daraException;
if (ResponseException.RetryAfter != null)
{
return Math.Min(ResponseException.RetryAfter.Value, maxDelay);
return (int)Math.Min(ResponseException.RetryAfter.Value, maxDelay);
}
}
if (retryCondition.Backoff == null)
{
return (long)DefaultMinDelay.TotalMilliseconds;
return (int)DefaultMinDelay.TotalMilliseconds;
}
var delayTimeMillis = retryCondition.Backoff.GetDelayTime(ctx);
long delayTime = delayTimeMillis != null ? (long)delayTimeMillis : (long)DefaultMinDelay.TotalMilliseconds;
return Math.Min(delayTime, maxDelay);
return (int)Math.Min(delayTime, maxDelay);
}
}
}
}
return (long)DefaultMinDelay.TotalMilliseconds;
return (int)DefaultMinDelay.TotalMilliseconds;
}

public static void Sleep(int backoffTime)
Expand Down Expand Up @@ -353,6 +353,18 @@ public static Stream BytesReadable(byte[] bytes)
return stream;
}

public static Dictionary<string, object> ToObject(IDictionary dictionary)
{
var result = new Dictionary<string, object>();

foreach (DictionaryEntry entry in dictionary)
{
result[entry.Key.ToString()] = entry.Value; // 将值存储为 object
}

return result;
}

internal static string PercentEncode(string value)
{
if (value == null)
Expand Down
12 changes: 12 additions & 0 deletions Darabonba/DaraRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,17 @@ public Dictionary<string, string> Headers
}

public Stream Body;

public Dictionary<string, object> ToMap(bool noStream = false)
{
var map = new Dictionary<string, object>();
return map;
}

public static DaraRequest FromMap(Dictionary<string, object> map)
{
var model = new DaraRequest();
return model;
}
}
}
13 changes: 13 additions & 0 deletions Darabonba/DaraResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,18 @@ public DaraResponse(HttpResponseMessage response)
_responseAsync = response;
}
}

public Dictionary<string, object> ToMap(bool noStream = false)
{
var map = new Dictionary<string, object>();
return map;
}

public static DaraResponse FromMap(Dictionary<string, object> map)
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
var model = new DaraResponse(httpResponseMessage);
return model;
}
}
}
9 changes: 4 additions & 5 deletions Darabonba/Models/RuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RuntimeOptions : DaraModel
public bool? IgnoreSSL { get; set; }
public string Key { get; set; }
public string Cert { get; set; }
public string CA { get; set; }
public string Ca { get; set; }
public int? MaxAttempts { get; set; }
public string BackoffPolicy { get; set; }
public int? BackoffPeriod { get; set; }
Expand Down Expand Up @@ -60,10 +60,10 @@ public Dictionary<string, object> ToMap(bool noStream = false)
{
map["cert"] = Cert;
}
if (CA != null)
if (Ca != null)
{
// TODO 是全小写吗
map["ca"] = CA;
map["ca"] = Ca;
}
if (MaxAttempts != null)
{
Expand Down Expand Up @@ -121,7 +121,6 @@ public Dictionary<string, object> ToMap(bool noStream = false)
{
map["extendsParameters"] = ExtendsParameters != null ? ExtendsParameters.ToMap(noStream) : null;
}

return map;
}

Expand All @@ -147,7 +146,7 @@ public static RuntimeOptions FromMap(Dictionary<string, object> map)
}
if (map.ContainsKey("ca"))
{
model.CA = (string)map["ca"];
model.Ca = (string)map["ca"];
}
if (map.ContainsKey("max_attempts"))
{
Expand Down
9 changes: 9 additions & 0 deletions Darabonba/RetryPolicy/RetryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ public class RetryOptions
public List<RetryCondition> RetryCondition { get; set; }
public List<RetryCondition> NoRetryCondition { get; set; }

public Dictionary<string, object> ToMap(bool noStream = false)
{
return new Dictionary<string, object>();
}

public static RetryOptions FromMap(Dictionary<string, object> map)
{
return new RetryOptions();
}
}
}
Loading

0 comments on commit 840a88a

Please sign in to comment.