From 7fc8de75cbbe771e283afe9f1cdcf22715e7bdcc Mon Sep 17 00:00:00 2001 From: Steve Waldman Date: Sat, 24 Feb 2024 21:37:39 -0500 Subject: [PATCH] Implement test utility com.mchange.v2.c3p0.test.Percent20FailConnectionTester --- test/resources-local/c3p0.properties | 3 +- .../c3p0/test/AlwaysFailConnectionTester.java | 7 +-- .../test/Percent20FailConnectionTester.java | 46 +++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java diff --git a/test/resources-local/c3p0.properties b/test/resources-local/c3p0.properties index 6df259c6..a21bb2bb 100644 --- a/test/resources-local/c3p0.properties +++ b/test/resources-local/c3p0.properties @@ -4,7 +4,7 @@ c3p0.testConnectionOnCheckout=true #c3p0.minPoolSize=10 #c3p0.maxPoolSize=40 #c3p0.checkoutTimeout=2000 -c3p0.connectionIsValidTimeout=5 +#c3p0.connectionIsValidTimeout=5 #c3p0.idleConnectionTestPeriod=30 #c3p0.idleConnectionTestPeriod=1 #c3p0.maxConnectionAge=10 @@ -31,6 +31,7 @@ c3p0.connectionIsValidTimeout=5 #c3p0.contextClassLoaderSource=library #c3p0.privilegeSpawnedThreads=true #c3p0.connectionTesterClassName=com.mchange.v2.c3p0.test.AlwaysFailConnectionTester +#c3p0.connectionTesterClassName=com.mchange.v2.c3p0.test.Percent20FailConnectionTester #c3p0.connectionTesterClassName=com.mchange.v2.c3p0.example.IsValidOnlyConnectionTester30 #c3p0.taskRunnerFactoryClassName=com.mchange.v2.c3p0.test.LogCreationTaskRunnerFactory #c3p0.taskRunnerFactoryClassName=com.mchange.v2.c3p0.test.ThreadPerTaskRunnerFactory diff --git a/test/src/com/mchange/v2/c3p0/test/AlwaysFailConnectionTester.java b/test/src/com/mchange/v2/c3p0/test/AlwaysFailConnectionTester.java index 8e4ecdfa..4641e051 100644 --- a/test/src/com/mchange/v2/c3p0/test/AlwaysFailConnectionTester.java +++ b/test/src/com/mchange/v2/c3p0/test/AlwaysFailConnectionTester.java @@ -67,10 +67,7 @@ public int activeCheckConnection(Connection c, String preferredTestQuery) return CONNECTION_IS_INVALID; } - public boolean equals( Object o ) - { return (o instanceof AlwaysFailConnectionTester); } - - public int hashCode() - { return 1; } + public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); } + public int hashCode() { return this.getClass().getName().hashCode(); } } diff --git a/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java b/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java new file mode 100644 index 00000000..18d9867f --- /dev/null +++ b/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java @@ -0,0 +1,46 @@ +package com.mchange.v2.c3p0.test; + +import java.sql.Connection; +import com.mchange.v2.c3p0.QueryConnectionTester; +import com.mchange.v2.log.MLevel; +import com.mchange.v2.log.MLog; +import com.mchange.v2.log.MLogger; + +public final class Percent20FailConnectionTester implements QueryConnectionTester +{ + final static MLogger logger = MLog.getLogger( Percent20FailConnectionTester.class ); + + { + //logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") ); + } + + private int roulette() + { + if (Math.random() < 0.20d) + return CONNECTION_IS_INVALID; + else + return CONNECTION_IS_OKAY; + } + + public int activeCheckConnection(Connection c) + { + //logger.warning(this + ": activeCheckConnection(Connection c)"); + return roulette(); + } + + public int statusOnException(Connection c, Throwable t) + { + //logger.warning(this + ": statusOnException(Connection c, Throwable t)"); + return roulette(); + } + + public int activeCheckConnection(Connection c, String preferredTestQuery) + { + //logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)"); + return roulette(); + } + + public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); } + public int hashCode() { return this.getClass().getName().hashCode(); } +} +