|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (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 |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.internal.connection; |
| 18 | + |
| 19 | +import com.mongodb.MongoCredential; |
| 20 | +import com.mongodb.MongoException; |
| 21 | +import com.mongodb.MongoSecurityException; |
| 22 | +import com.mongodb.MongoSocketException; |
| 23 | +import com.mongodb.MongoSocketOpenException; |
| 24 | +import com.mongodb.MongoSocketReadTimeoutException; |
| 25 | +import com.mongodb.ServerAddress; |
| 26 | +import org.bson.BsonDocument; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import javax.net.ssl.SSLHandshakeException; |
| 30 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 31 | +import java.io.IOException; |
| 32 | +import java.net.UnknownHostException; |
| 33 | +import java.security.cert.CertificateException; |
| 34 | + |
| 35 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 37 | + |
| 38 | +class BackpressureErrorLabelerTest { |
| 39 | + |
| 40 | + private static final ServerAddress ADDRESS = new ServerAddress(); |
| 41 | + |
| 42 | + @Test |
| 43 | + void mongoSocketExceptionIsLabeled() { |
| 44 | + MongoSocketException e = new MongoSocketException("boom", ADDRESS); |
| 45 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 46 | + assertHasBackpressureLabels(e); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void mongoSocketReadTimeoutIsLabeled() { |
| 51 | + MongoSocketReadTimeoutException e = new MongoSocketReadTimeoutException("slow", ADDRESS, new IOException("read timed out")); |
| 52 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 53 | + assertHasBackpressureLabels(e); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void mongoSocketOpenExceptionIsLabeled() { |
| 58 | + MongoSocketOpenException e = new MongoSocketOpenException("open failed", ADDRESS, new IOException("connection refused")); |
| 59 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 60 | + assertHasBackpressureLabels(e); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void dnsFailureIsNotLabeled() { |
| 65 | + MongoSocketException e = new MongoSocketException("lookup failed", ADDRESS, new UnknownHostException("nope")); |
| 66 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 67 | + assertLacksBackpressureLabels(e); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void dnsFailureDeepInCauseChainIsNotLabeled() { |
| 72 | + Throwable dns = new UnknownHostException("nope"); |
| 73 | + IOException wrap1 = new IOException("wrap1", dns); |
| 74 | + MongoSocketException e = new MongoSocketException("wrap2", ADDRESS, wrap1); |
| 75 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 76 | + assertLacksBackpressureLabels(e); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void tlsCertificateErrorIsNotLabeled() { |
| 81 | + CertificateException cert = new CertificateException("bad cert"); |
| 82 | + MongoSocketException e = new MongoSocketException("tls", ADDRESS, cert); |
| 83 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 84 | + assertLacksBackpressureLabels(e); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void tlsPeerUnverifiedIsNotLabeled() { |
| 89 | + SSLPeerUnverifiedException tls = new SSLPeerUnverifiedException("peer not verified"); |
| 90 | + MongoSocketException e = new MongoSocketException("tls", ADDRESS, tls); |
| 91 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 92 | + assertLacksBackpressureLabels(e); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void sslHandshakeWithCertificateMessageIsNotLabeled() { |
| 97 | + SSLHandshakeException tls = new SSLHandshakeException("PKIX path building failed: certificate chain invalid"); |
| 98 | + MongoSocketException e = new MongoSocketException("tls", ADDRESS, tls); |
| 99 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 100 | + assertLacksBackpressureLabels(e); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void sslHandshakeWithoutConfigKeywordIsLabeled() { |
| 105 | + SSLHandshakeException tls = new SSLHandshakeException("remote host closed connection during handshake"); |
| 106 | + MongoSocketException e = new MongoSocketException("tls", ADDRESS, tls); |
| 107 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 108 | + assertHasBackpressureLabels(e); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void authErrorIsNotLabeled() { |
| 113 | + MongoCredential credential = MongoCredential.createCredential("user", "db", "pwd".toCharArray()); |
| 114 | + MongoSecurityException e = new MongoSecurityException(credential, "auth failed"); |
| 115 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 116 | + assertLacksBackpressureLabels(e); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void commandExceptionIsNotLabeled() { |
| 121 | + MongoException e = new MongoException(42, "some command error"); |
| 122 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 123 | + assertLacksBackpressureLabels(e); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void nonMongoThrowableIsNotLabeled() { |
| 128 | + IOException e = new IOException("raw"); |
| 129 | + BackpressureErrorLabeler.applyLabelsIfEligible(e); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void sdamIssueTlsCheckDelegatesToHelper() { |
| 134 | + MongoSocketException tlsException = new MongoSocketException("tls", ADDRESS, new CertificateException("bad cert")); |
| 135 | + assertTrue(BackpressureErrorLabeler.isTlsConfigurationError(tlsException)); |
| 136 | + |
| 137 | + MongoSocketException plainException = new MongoSocketException("plain", ADDRESS); |
| 138 | + assertFalse(BackpressureErrorLabeler.isTlsConfigurationError(plainException)); |
| 139 | + |
| 140 | + assertFalse(BackpressureErrorLabeler.isTlsConfigurationError(new MongoException(0, "not socket", new BsonDocument()))); |
| 141 | + } |
| 142 | + |
| 143 | + private static void assertHasBackpressureLabels(final MongoException e) { |
| 144 | + assertTrue(e.hasErrorLabel(MongoException.SYSTEM_OVERLOADED_ERROR_LABEL), |
| 145 | + "expected SystemOverloadedError label"); |
| 146 | + assertTrue(e.hasErrorLabel(MongoException.RETRYABLE_ERROR_LABEL), |
| 147 | + "expected RetryableError label"); |
| 148 | + } |
| 149 | + |
| 150 | + private static void assertLacksBackpressureLabels(final MongoException e) { |
| 151 | + assertFalse(e.hasErrorLabel(MongoException.SYSTEM_OVERLOADED_ERROR_LABEL), |
| 152 | + "unexpected SystemOverloadedError label"); |
| 153 | + assertFalse(e.hasErrorLabel(MongoException.RETRYABLE_ERROR_LABEL), |
| 154 | + "unexpected RetryableError label"); |
| 155 | + } |
| 156 | +} |
0 commit comments