OLD | NEW |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 } | 701 } |
702 | 702 |
703 /// <summary>· | 703 /// <summary>· |
704 /// Tests that back-off handler works as expected when the server return
s 5xx and the maximum time span is set | 704 /// Tests that back-off handler works as expected when the server return
s 5xx and the maximum time span is set |
705 /// to 10 hours. | 705 /// to 10 hours. |
706 /// </summary> | 706 /// </summary> |
707 [Test] | 707 [Test] |
708 public void SendAsync_BackOffUnsuccessfulResponseHandler_ServiceUnavaila
ble_Max10Hours() | 708 public void SendAsync_BackOffUnsuccessfulResponseHandler_ServiceUnavaila
ble_Max10Hours() |
709 { | 709 { |
710 var initializer = new BackOffHandler.Initializer(new ExponentialBack
Off(TimeSpan.Zero)) | 710 var initializer = new BackOffHandler.Initializer(new ExponentialBack
Off(TimeSpan.Zero)) |
711 { | 711 { |
712 MaxTimeSpan = TimeSpan.FromHours(10) | 712 MaxTimeSpan = TimeSpan.FromHours(10) |
713 }; | 713 }; |
714 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.S
erviceUnavailable, initializer); | 714 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.S
erviceUnavailable, initializer); |
715 } | 715 } |
716 | 716 |
717 /// <summary>· | 717 /// <summary>· |
718 /// Tests that back-off handler isn't be called when the server returns
a successful response. | 718 /// Tests that back-off handler isn't be called when the server returns
a successful response. |
719 /// </summary> | 719 /// </summary> |
720 [Test] | 720 [Test] |
721 public void SendAsync_BackOffUnsuccessfulResponseHandler_OK() | 721 public void SendAsync_BackOffUnsuccessfulResponseHandler_OK() |
722 { | 722 { |
723 var initializer = new BackOffHandler.Initializer(new ExponentialBack
Off(TimeSpan.Zero)); | 723 var initializer = new BackOffHandler.Initializer(new ExponentialBack
Off(TimeSpan.Zero)); |
724 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.O
K, initializer); | 724 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.O
K, initializer); |
725 } | 725 } |
726 | 726 |
727 /// <summary> Tests that back-off handler is canceled when cancellation
token is used.</summary> | 727 /// <summary> Tests that back-off handler is canceled when cancellation
token is used.</summary> |
728 [Test] | 728 [Test] |
729 public void SendAsync_BackOffUnsuccessfulResponseHandler_Cancel() | 729 public void SendAsync_BackOffUnsuccessfulResponseHandler_Cancel() |
730 { | 730 { |
731 var initializer = new BackOffHandler.Initializer(new ExponentialBack
Off(TimeSpan.Zero)); | 731 // test back-off with maximum 30 minutes per single request |
| 732 var initializer = new BackOffHandler.Initializer(new ExponentialBack
Off(TimeSpan.Zero)) |
| 733 { |
| 734 MaxTimeSpan = TimeSpan.FromMinutes(30) |
| 735 }; |
732 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.S
erviceUnavailable, initializer, 2); | 736 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.S
erviceUnavailable, initializer, 2); |
733 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.S
erviceUnavailable, initializer, 6); | 737 SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.S
erviceUnavailable, initializer, 6); |
734 } | 738 } |
735 | 739 |
736 /// <summary>· | 740 /// <summary>· |
737 /// Subtest that back-off handler works as expected when a successful or
abnormal response is returned. | 741 /// Subtest that back-off handler works as expected when a successful or
abnormal response is returned. |
738 /// For testing the back-off handler in case of a canceled request, set
the <code>cancelRequestNum</code> | 742 /// For testing the back-off handler in case of a canceled request, set
the <code>cancelRequestNum</code> |
739 /// parameter to the index of the request you want to cancel. | 743 /// parameter to the index of the request you want to cancel. |
740 /// </summary> | 744 /// </summary> |
741 private void SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpSta
tusCode statusCode, | 745 private void SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpSta
tusCode statusCode, |
742 BackOffHandler.Initializer initializer, int cancelRequestNum = 0) | 746 BackOffHandler.Initializer initializer, int cancelRequestNum = 0, in
t numTries = 10) |
743 { | 747 { |
744 var handler = new UnsuccessfulResponseMessageHandler { ResponseStatu
sCode = statusCode }; | 748 var handler = new UnsuccessfulResponseMessageHandler { ResponseStatu
sCode = statusCode }; |
745 | 749 |
746 CancellationToken cancellationToken = CancellationToken.None; | 750 CancellationToken cancellationToken = CancellationToken.None; |
747 bool cancel = cancelRequestNum > 0; | 751 bool cancel = cancelRequestNum > 0; |
748 | 752 |
749 if (cancel) | 753 if (cancel) |
750 { | 754 { |
751 CancellationTokenSource tcs = new CancellationTokenSource(); | 755 CancellationTokenSource tcs = new CancellationTokenSource(); |
752 handler.CancellationTokenSource = tcs; | 756 handler.CancellationTokenSource = tcs; |
753 handler.CancelRequestNum = cancelRequestNum; | 757 handler.CancelRequestNum = cancelRequestNum; |
754 cancellationToken = tcs.Token; | 758 cancellationToken = tcs.Token; |
755 } | 759 } |
756 | 760 |
757 var configurableHanlder = new ConfigurableMessageHandler(handler); | 761 var configurableHanlder = new ConfigurableMessageHandler(handler) |
| 762 { |
| 763 NumTries = numTries |
| 764 }; |
758 var boHandler = new MockBackOffHandler(initializer); | 765 var boHandler = new MockBackOffHandler(initializer); |
759 configurableHanlder.UnsuccessfulResponseHandlers.Add(boHandler); | 766 configurableHanlder.UnsuccessfulResponseHandlers.Add(boHandler); |
760 | 767 |
761 int boHandleCount = 0; | 768 int boHandleCount = 0; |
762 if (initializer.HandleUnsuccessfulResponseFunc != null && | 769 if (initializer.HandleUnsuccessfulResponseFunc != null && |
763 initializer.HandleUnsuccessfulResponseFunc(new HttpResponseMessa
ge { StatusCode = statusCode })) | 770 initializer.HandleUnsuccessfulResponseFunc(new HttpResponseMessa
ge { StatusCode = statusCode })) |
764 { | 771 { |
765 boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxT
imeSpan.TotalSeconds, 2)) + 1, | 772 boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxT
imeSpan.TotalSeconds, 2)) + 1, |
766 configurableHanlder.NumTries - 1); | 773 configurableHanlder.NumTries - 1); |
767 boHandleCount = boHandleCount >= 0 ? boHandleCount : 0; | 774 boHandleCount = boHandleCount >= 0 ? boHandleCount : 0; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 String, | 840 String, |
834 Stream, | 841 Stream, |
835 ByteArray | 842 ByteArray |
836 } | 843 } |
837 | 844 |
838 /// <summary> Tests that retry works with different kind of contents (St
ring, Stream and ByteArray). </summary> | 845 /// <summary> Tests that retry works with different kind of contents (St
ring, Stream and ByteArray). </summary> |
839 private void SubtestSendAsyncRetryContent(ContentType type) | 846 private void SubtestSendAsyncRetryContent(ContentType type) |
840 { | 847 { |
841 var content = "test-content"; | 848 var content = "test-content"; |
842 var contentHandler = new ContentMessageHandler(); | 849 var contentHandler = new ContentMessageHandler(); |
843 var configurableHanlder = new ConfigurableMessageHandler(contentHand
ler); | 850 var configurableHanlder = new ConfigurableMessageHandler(contentHand
ler) |
| 851 { |
| 852 NumTries = 10 |
| 853 }; |
844 configurableHanlder.UnsuccessfulResponseHandlers.Add(new TrueUnsucce
ssfulResponseHandler()); | 854 configurableHanlder.UnsuccessfulResponseHandlers.Add(new TrueUnsucce
ssfulResponseHandler()); |
845 using (var client = new HttpClient(configurableHanlder)) | 855 using (var client = new HttpClient(configurableHanlder)) |
846 { | 856 { |
847 var request = new HttpRequestMessage(HttpMethod.Put, "https://te
st-unsuccessful-handler"); | 857 var request = new HttpRequestMessage(HttpMethod.Put, "https://te
st-unsuccessful-handler"); |
848 // set the right content | 858 // set the right content |
849 switch (type) | 859 switch (type) |
850 { | 860 { |
851 case ContentType.String: | 861 case ContentType.String: |
852 request.Content = new StringContent(content); | 862 request.Content = new StringContent(content); |
853 break; | 863 break; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
893 SubtestSendAsyncRetryContent(ContentType.ByteArray); | 903 SubtestSendAsyncRetryContent(ContentType.ByteArray); |
894 } | 904 } |
895 | 905 |
896 #endregion | 906 #endregion |
897 | 907 |
898 /// <summary> Tests setting number of tries. </summary> | 908 /// <summary> Tests setting number of tries. </summary> |
899 [Test] | 909 [Test] |
900 public void NumTries_Setter() | 910 public void NumTries_Setter() |
901 { | 911 { |
902 var configurableHanlder = new ConfigurableMessageHandler(new HttpCli
entHandler()); | 912 var configurableHanlder = new ConfigurableMessageHandler(new HttpCli
entHandler()); |
| 913 |
903 // valid values | 914 // valid values |
904 configurableHanlder.NumTries = 5; | 915 configurableHanlder.NumTries = ConfigurableMessageHandler.MaxAllowed
NumTries; |
| 916 configurableHanlder.NumTries = ConfigurableMessageHandler.MaxAllowed
NumTries - 1; |
905 configurableHanlder.NumTries = 1; | 917 configurableHanlder.NumTries = 1; |
906 configurableHanlder.NumTries = ConfigurableMessageHandler.MaxAllowed
NumTries; | |
907 | 918 |
908 // test invalid values | 919 // test invalid values |
909 try | 920 try |
910 { | 921 { |
911 configurableHanlder.NumTries = ConfigurableMessageHandler.MaxAll
owedNumTries + 1; | 922 configurableHanlder.NumTries = ConfigurableMessageHandler.MaxAll
owedNumTries + 1; |
912 Assert.Fail(); | 923 Assert.Fail(); |
913 } | 924 } |
914 catch (ArgumentOutOfRangeException ex) | 925 catch (ArgumentOutOfRangeException ex) |
915 { | 926 { |
916 Assert.True(ex.Message.Contains("Parameter name: NumRetries")); | 927 Assert.True(ex.Message.Contains("Parameter name: NumTries")); |
917 } | 928 } |
918 try | 929 try |
919 { | 930 { |
920 configurableHanlder.NumTries = 0; | 931 configurableHanlder.NumTries = 0; |
921 Assert.Fail(); | 932 Assert.Fail(); |
922 } | 933 } |
923 catch (ArgumentOutOfRangeException ex) | 934 catch (ArgumentOutOfRangeException ex) |
924 { | 935 { |
925 Assert.True(ex.Message.Contains("Parameter name: NumRetries")); | 936 Assert.True(ex.Message.Contains("Parameter name: NumTries")); |
926 } | 937 } |
927 try | 938 try |
928 { | 939 { |
929 configurableHanlder.NumTries = -2; | 940 configurableHanlder.NumTries = -2; |
930 Assert.Fail(); | 941 Assert.Fail(); |
931 } | 942 } |
932 catch (ArgumentOutOfRangeException ex) | 943 catch (ArgumentOutOfRangeException ex) |
933 { | 944 { |
934 Assert.True(ex.Message.Contains("Parameter name: NumRetries")); | 945 Assert.True(ex.Message.Contains("Parameter name: NumTries")); |
935 } | 946 } |
936 } | 947 } |
937 | 948 |
938 /// <summary>· | 949 /// <summary>· |
939 /// Tests the number of tries in case of unsuccessful response when unsu
ccessful response handler is plugged to· | 950 /// Tests the number of tries in case of unsuccessful response when unsu
ccessful response handler is plugged to· |
940 /// the message handler.· | 951 /// the message handler.· |
941 /// </summary> | 952 /// </summary> |
942 [Test] | 953 [Test] |
943 public void SendAsync_NumTries() | 954 public void SendAsync_NumTries() |
944 { | 955 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1000 // with application name | 1011 // with application name |
1001 configurableHanlder.ApplicationName = applicationName; | 1012 configurableHanlder.ApplicationName = applicationName; |
1002 request = new HttpRequestMessage(HttpMethod.Get, "https://test-u
ser-agent"); | 1013 request = new HttpRequestMessage(HttpMethod.Get, "https://test-u
ser-agent"); |
1003 response = client.SendAsync(request).Result; | 1014 response = client.SendAsync(request).Result; |
1004 userAgent = string.Join(" ", request.Headers.GetValues("User-Age
nt").ToArray()); | 1015 userAgent = string.Join(" ", request.Headers.GetValues("User-Age
nt").ToArray()); |
1005 Assert.That(userAgent, Is.EqualTo(applicationName + " " + apiVer
sion)); | 1016 Assert.That(userAgent, Is.EqualTo(applicationName + " " + apiVer
sion)); |
1006 } | 1017 } |
1007 } | 1018 } |
1008 } | 1019 } |
1009 } | 1020 } |
OLD | NEW |