Skip to content

Commit

Permalink
Implement test utility com.mchange.v2.c3p0.test.Percent20FailConnecti…
Browse files Browse the repository at this point in the history
…onTester
  • Loading branch information
swaldman committed Feb 25, 2024
1 parent 9abd109 commit 7fc8de7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
3 changes: 2 additions & 1 deletion test/resources-local/c3p0.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
}

Original file line number Diff line number Diff line change
@@ -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(); }
}

0 comments on commit 7fc8de7

Please sign in to comment.