[jboss-svn-commits] JBL Code SVN: r31605 - in labs/jbosstm/trunk/ArjunaCore/arjuna: tests/classes/com/hp/mwtests/ts/arjuna/common and 3 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Feb 12 08:11:46 EST 2010
Author: mark.little at jboss.com
Date: 2010-02-12 08:11:46 -0500 (Fri, 12 Feb 2010)
New Revision: 31605
Added:
labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLockingUnitTest.java
labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/recovery/TransactionStatusConnectionManagerUnitTest.java
Removed:
labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLocking.java
Modified:
labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/FileLock.java
labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/common/TypesUnitTest.java
labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/state/IOStateUnitTest.java
Log:
https://jira.jboss.org/jira/browse/JBTM-698
Modified: labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/FileLock.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/FileLock.java 2010-02-12 08:49:08 UTC (rev 31604)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/FileLock.java 2010-02-12 13:11:46 UTC (rev 31605)
@@ -42,347 +42,375 @@
import com.arjuna.common.util.logging.*;
/**
- * Sometimes it is necessary to lock a file at the disk level.
- *
- * Since there is no native Java way of locking a file, we
- * have to implement our own. Unfortunately, it appears as though
- * we can only assume that rename is atomic. We base the locking on
- * this then: rename the lock file and update it with the lock owners.
- *
- * How it works:
- *
- * for every file we want to lock we create an _lock file. This file
- * contains information about who is locking the file, and in what mode.
- * (Single writer, multiple readers.) To guarantee atomicity of update, we
- * move (rename) the actual file each time we want to lock it and update the
- * lock file. When this is done, we move (rename) it back. Almost like a
- * two-phase commit protocol! Currently we don't support re-entrant locking.
- *
+ * Sometimes it is necessary to lock a file at the disk level. Since there is no
+ * native Java way of locking a file, we have to implement our own.
+ * Unfortunately, it appears as though we can only assume that rename is atomic.
+ * We base the locking on this then: rename the lock file and update it with the
+ * lock owners. How it works: for every file we want to lock we create an _lock
+ * file. This file contains information about who is locking the file, and in
+ * what mode. (Single writer, multiple readers.) To guarantee atomicity of
+ * update, we move (rename) the actual file each time we want to lock it and
+ * update the lock file. When this is done, we move (rename) it back. Almost
+ * like a two-phase commit protocol! Currently we don't support re-entrant
+ * locking.
+ *
* @author Mark Little (mark at arjuna.com)
- * @version $Id: FileLock.java 2342 2006-03-30 13:06:17Z $
+ * @version $Id: FileLock.java 2342 2006-03-30 13:06:17Z $
* @since JTS 1.0.
- *
- * @message com.arjuna.ats.arjuna.utils.FileLock_1 [com.arjuna.ats.arjuna.utils.FileLock_1] - FileLock.lock called for {0}
- * @message com.arjuna.ats.arjuna.utils.FileLock_2 [com.arjuna.ats.arjuna.utils.FileLock_2] - FileLock.unlock called for {0}
- * @message com.arjuna.ats.arjuna.utils.FileLock_3 [com.arjuna.ats.arjuna.utils.FileLock_3] - FileLock.createFile called for {0}
- * @message com.arjuna.ats.arjuna.utils.FileLock_4 [com.arjuna.ats.arjuna.utils.FileLock_4] - An error occurred while creating file {0}
- * @message com.arjuna.ats.arjuna.utils.FileLock_5 [com.arjuna.ats.arjuna.utils.FileLock_5] - FileLock.lockFile called for {0}
- * @message com.arjuna.ats.arjuna.utils.FileLock_6 [com.arjuna.ats.arjuna.utils.FileLock_6] - FileLock.unlockFile called for {0}
+ * @message com.arjuna.ats.arjuna.utils.FileLock_1
+ * [com.arjuna.ats.arjuna.utils.FileLock_1] - FileLock.lock called for
+ * {0}
+ * @message com.arjuna.ats.arjuna.utils.FileLock_2
+ * [com.arjuna.ats.arjuna.utils.FileLock_2] - FileLock.unlock called
+ * for {0}
+ * @message com.arjuna.ats.arjuna.utils.FileLock_3
+ * [com.arjuna.ats.arjuna.utils.FileLock_3] - FileLock.createFile
+ * called for {0}
+ * @message com.arjuna.ats.arjuna.utils.FileLock_4
+ * [com.arjuna.ats.arjuna.utils.FileLock_4] - An error occurred while
+ * creating file {0}
+ * @message com.arjuna.ats.arjuna.utils.FileLock_5
+ * [com.arjuna.ats.arjuna.utils.FileLock_5] - FileLock.lockFile called
+ * for {0}
+ * @message com.arjuna.ats.arjuna.utils.FileLock_6
+ * [com.arjuna.ats.arjuna.utils.FileLock_6] - FileLock.unlockFile
+ * called for {0}
*/
public class FileLock
{
-public static final int F_RDLCK = 0;
-public static final int F_WRLCK = 1;
+ public static final int F_RDLCK = 0;
-public static final int defaultTimeout = 10; // milliseconds
-public static final int defaultRetry = 10;
-
-public FileLock (File name)
+ public static final int F_WRLCK = 1;
+
+ public static final int defaultTimeout = 10; // milliseconds
+
+ public static final int defaultRetry = 10;
+
+ public FileLock (String name)
{
- if (tsLogger.arjLogger.isDebugEnabled())
- {
- tsLogger.arjLogger.debug(DebugLevel.CONSTRUCTORS,
- VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "FileLock ( "+name+" )");
- }
-
- _theFile = name;
- _lockFile = new File(name.toString() + "_lock");
- _lockFileLock = new File(name.toString() + "_lock.lock");
- _timeout = FileLock.defaultTimeout;
- _retry = FileLock.defaultRetry;
+ this(new File(name));
+
+ _theFile.deleteOnExit();
}
+
+ public FileLock(File name)
+ {
+ this(name, FileLock.defaultTimeout, FileLock.defaultRetry);
+ }
-public FileLock (File name, long timeout, long retry)
+ public FileLock(File name, long timeout, long retry)
{
- if (tsLogger.arjLogger.isDebugEnabled())
- {
- tsLogger.arjLogger.debug(DebugLevel.CONSTRUCTORS,
- VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "FileLock ( "+name+", "+timeout+", "+retry+" )");
- }
-
- _theFile = name;
- _lockFile = new File(name.toString() + "_lock");
- _lockFileLock = new File(name.toString() + "_lock.lock");
- _timeout = timeout;
- _retry = retry;
+ if (tsLogger.arjLogger.isDebugEnabled())
+ {
+ tsLogger.arjLogger
+ .debug(DebugLevel.CONSTRUCTORS, VisibilityLevel.VIS_PUBLIC,
+ FacilityCode.FAC_GENERAL, "FileLock ( " + name
+ + ", " + timeout + ", " + retry + " )");
+ }
+
+ _theFile = name;
+ _lockFile = new File(name.toString() + "_lock");
+ _lockFileLock = new File(name.toString() + "_lock.lock");
+ _timeout = timeout;
+ _retry = retry;
+
+ _lockFile.deleteOnExit();
+ _lockFileLock.deleteOnExit();
}
/**
* @since JTS 2.1.1.
*/
-public boolean lock (int lmode)
+ public boolean lock (int lmode)
{
- return lock(lmode, false);
+ return lock(lmode, false);
}
-
-public boolean lock (int lmode, boolean create)
+
+ public synchronized boolean lock (int lmode, boolean create)
{
- if (tsLogger.arjLoggerI18N.isDebugEnabled())
- {
- tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS, VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "com.arjuna.ats.arjuna.utils.FileLock_1",
- new Object[]{_lockFile});
- }
+ if (tsLogger.arjLoggerI18N.isDebugEnabled())
+ {
+ tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS,
+ VisibilityLevel.VIS_PUBLIC, FacilityCode.FAC_GENERAL,
+ "com.arjuna.ats.arjuna.utils.FileLock_1", new Object[]
+ { _lockFile });
+ }
- if (create && !_theFile.exists())
- {
- createFile();
- }
-
- /*
- * If the lock file exists, and the mode is exclusive, then we can
- * immediately return false.
- *
- * Currently we do not implement re-entrant locking, which requires
- * some owner id.
- */
+ boolean created = false;
+
+ if (create && !_theFile.exists())
+ {
+ createFile();
+
+ created = true;
+ }
- if (_lockFile.exists() && (lmode == FileLock.F_WRLCK))
- return false;
+ /*
+ * If the lock file exists, and the mode is exclusive, then we can
+ * immediately return false. Currently we do not implement re-entrant
+ * locking, which requires some owner id.
+ */
- int number = 0;
+ if (!created && _lockFile.exists() && (lmode == FileLock.F_WRLCK))
+ return false;
- if (lockFile()) // have we moved the file (if it exists)?
- {
- try
- {
- DataInputStream ifile = new DataInputStream(new FileInputStream(_lockFile));
- int value = ifile.readInt();
-
- /*
- * Already exclusively locked.
- */
-
- if (value == FileLock.F_WRLCK)
- {
- ifile.close();
- unlockFile();
-
- return false;
- }
- else
- number = ifile.readInt();
+ int number = 0;
- ifile.close();
- }
- catch (FileNotFoundException e)
- {
- }
- catch (IOException e)
- {
- /*
- * Something went wrong. Abandon.
- */
+ if (lockFile()) // have we moved the file (if it exists)?
+ {
+ try
+ {
+ DataInputStream ifile = new DataInputStream(
+ new FileInputStream(_lockFile));
+ int value = ifile.readInt();
- unlockFile();
+ /*
+ * Already exclusively locked.
+ */
- return false;
- }
+ if (value == FileLock.F_WRLCK)
+ {
+ ifile.close();
+ unlockFile();
- try
- {
- DataOutputStream ofile = new DataOutputStream(new FileOutputStream(_lockFile));
+ return false;
+ }
+ else
+ number = ifile.readInt();
- number++;
-
- ofile.writeInt(lmode);
- ofile.writeInt(number);
+ ifile.close();
+ }
+ catch (FileNotFoundException e)
+ {
+ }
+ catch (IOException e)
+ {
+ /*
+ * Something went wrong. Abandon.
+ */
- ofile.close();
+ unlockFile();
- unlockFile();
-
- return true;
- }
- catch (IOException e)
- {
- /*
- * Something went wrong. Abandon.
- * Lock file is ok since we haven't touched it.
- */
+ return false;
+ }
- unlockFile();
+ try
+ {
+ DataOutputStream ofile = new DataOutputStream(
+ new FileOutputStream(_lockFile));
- return false;
- }
- }
+ number++;
- return false;
+ ofile.writeInt(lmode);
+ ofile.writeInt(number);
+
+ ofile.close();
+
+ unlockFile();
+
+ return true;
+ }
+ catch (IOException e)
+ {
+ /*
+ * Something went wrong. Abandon. Lock file is ok since we
+ * haven't touched it.
+ */
+
+ unlockFile();
+
+ return false;
+ }
+ }
+
+ return false;
}
-public boolean unlock ()
+ public synchronized boolean unlock ()
{
- if (tsLogger.arjLoggerI18N.isDebugEnabled())
- {
- tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS, VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "com.arjuna.ats.arjuna.utils.FileLock_2",
- new Object[]{_lockFile});
- }
-
- if (!_lockFile.exists())
- return false;
+ if (tsLogger.arjLoggerI18N.isDebugEnabled())
+ {
+ tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS,
+ VisibilityLevel.VIS_PUBLIC, FacilityCode.FAC_GENERAL,
+ "com.arjuna.ats.arjuna.utils.FileLock_2", new Object[]
+ { _lockFile });
+ }
- if (lockFile())
- {
- int number = 0, mode = 0;
-
- try
- {
- DataInputStream ifile = new DataInputStream(new FileInputStream(_lockFile));
+ if (!_lockFile.exists())
+ return false;
- mode = ifile.readInt();
- number = ifile.readInt();
- ifile.close();
+ if (lockFile())
+ {
+ int number = 0, mode = 0;
- number--;
+ try
+ {
+ DataInputStream ifile = new DataInputStream(
+ new FileInputStream(_lockFile));
- if (number == 0)
- {
- _lockFile.delete();
+ mode = ifile.readInt();
+ number = ifile.readInt();
+ ifile.close();
- unlockFile();
+ number--;
- return true;
- }
- }
- catch (FileNotFoundException e)
- {
- unlockFile();
+ if (number == 0)
+ {
+ _lockFile.delete();
- return false;
- }
- catch (IOException e)
- {
- unlockFile();
+ unlockFile();
- return false;
- }
-
- try
- {
- DataOutputStream ofile = new DataOutputStream(new FileOutputStream(_lockFile));
+ return true;
+ }
+ }
+ catch (FileNotFoundException e)
+ {
+ unlockFile();
- ofile.writeInt(mode);
- ofile.writeInt(number);
- ofile.close();
-
- unlockFile();
-
- return true;
- }
- catch (IOException e)
- {
- unlockFile();
-
- return false;
- }
- }
+ return false;
+ }
+ catch (IOException e)
+ {
+ unlockFile();
- return false;
+ return false;
+ }
+
+ try
+ {
+ DataOutputStream ofile = new DataOutputStream(
+ new FileOutputStream(_lockFile));
+
+ ofile.writeInt(mode);
+ ofile.writeInt(number);
+ ofile.close();
+
+ unlockFile();
+
+ return true;
+ }
+ catch (IOException e)
+ {
+ unlockFile();
+
+ return false;
+ }
+ }
+
+ return false;
}
-public static String modeString (int mode)
+ public static String modeString (int mode)
{
- switch (mode)
- {
- case FileLock.F_RDLCK:
- return "FileLock.F_RDLCK";
- case FileLock.F_WRLCK:
- return "FileLock.F_WRLCK";
- default:
- return "Unknown";
- }
+ switch (mode)
+ {
+ case FileLock.F_RDLCK:
+ return "FileLock.F_RDLCK";
+ case FileLock.F_WRLCK:
+ return "FileLock.F_WRLCK";
+ default:
+ return "Unknown";
+ }
}
-private final boolean createFile ()
+ private final boolean createFile ()
{
- if (tsLogger.arjLoggerI18N.isDebugEnabled())
- {
- tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS, VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "com.arjuna.ats.arjuna.utils.FileLock_3",
- new Object[]{_lockFile});
- }
+ if (tsLogger.arjLoggerI18N.isDebugEnabled())
+ {
+ tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS,
+ VisibilityLevel.VIS_PUBLIC, FacilityCode.FAC_GENERAL,
+ "com.arjuna.ats.arjuna.utils.FileLock_3", new Object[]
+ { _lockFile });
+ }
- byte b[] = new byte[1];
+ byte b[] = new byte[1];
- try
- {
- if (!_theFile.exists())
- {
- _theFile.createNewFile();
+ try
+ {
+ if (!_theFile.exists())
+ {
+ _theFile.createNewFile();
- return true;
- }
- else
- return false;
- }
- catch (IOException e)
- {
-
- if (tsLogger.arjLoggerI18N.isWarnEnabled())
- {
- tsLogger.arjLoggerI18N.warn("com.arjuna.ats.arjuna.utils.FileLock_4", new Object[]{_lockFile});
- }
+ return true;
+ }
+ else
+ return false;
+ }
+ catch (IOException e)
+ {
- return false;
- }
+ if (tsLogger.arjLoggerI18N.isWarnEnabled())
+ {
+ tsLogger.arjLoggerI18N.warn(
+ "com.arjuna.ats.arjuna.utils.FileLock_4", new Object[]
+ { _lockFile });
+ }
+
+ return false;
+ }
}
-
-private final boolean lockFile ()
+
+ private final boolean lockFile ()
{
- if (tsLogger.arjLoggerI18N.isDebugEnabled())
- {
- tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS, VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "com.arjuna.ats.arjuna.utils.FileLock_5",
- new Object[]{_lockFile});
- }
+ if (tsLogger.arjLoggerI18N.isDebugEnabled())
+ {
+ tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS,
+ VisibilityLevel.VIS_PUBLIC, FacilityCode.FAC_GENERAL,
+ "com.arjuna.ats.arjuna.utils.FileLock_5", new Object[]
+ { _lockFile });
+ }
+
+ for (int i = 0; i < _retry; i++)
+ {
+ try
+ {
+ if (_lockFileLock.createNewFile())
+ {
+ return true;
+ }
+ else
+ {
+ try
+ {
+ Thread.sleep(_timeout);
+ }
+ catch (InterruptedException e)
+ {
+ }
+ }
+ }
+ catch (IOException ex)
+ {
+ // already created, so locked!
+ }
+ }
- for (int i = 0; i < _retry; i++)
- {
- try
- {
- if (_lockFileLock.createNewFile())
- return true;
- else
- {
- try
- {
- Thread.sleep(_timeout);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- catch (IOException ex)
- {
- // already created, so locked!
- }
- }
-
- return false;
+ return false;
}
-private final boolean unlockFile ()
+ private final boolean unlockFile ()
{
- if (tsLogger.arjLoggerI18N.isDebugEnabled())
- {
- tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS, VisibilityLevel.VIS_PUBLIC,
- FacilityCode.FAC_GENERAL, "com.arjuna.ats.arjuna.utils.FileLock_6",
- new Object[]{_lockFile});
- }
- return _lockFileLock.delete();
+ if (tsLogger.arjLoggerI18N.isDebugEnabled())
+ {
+ tsLogger.arjLoggerI18N.debug(DebugLevel.FUNCTIONS,
+ VisibilityLevel.VIS_PUBLIC, FacilityCode.FAC_GENERAL,
+ "com.arjuna.ats.arjuna.utils.FileLock_6", new Object[]
+ { _lockFile });
+ }
+ return _lockFileLock.delete();
}
-
+
private File _theFile;
+
private File _lockFile;
+
private File _lockFileLock;
+
private long _timeout;
+
private long _retry;
-
+
}
Modified: labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/common/TypesUnitTest.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/common/TypesUnitTest.java 2010-02-12 08:49:08 UTC (rev 31604)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/common/TypesUnitTest.java 2010-02-12 13:11:46 UTC (rev 31605)
@@ -156,5 +156,10 @@
assertEquals(RecordType.classToType(PersistenceRecord.class), RecordType.PERSISTENCE);
assertEquals(RecordType.typeToClass(RecordType.ACTIVATION), ActivationRecord.class);
+
+ assertEquals(RecordType.classToType(RecordType.class), RecordType.UNTYPED);
+
+ for (int i = 0; i < 500; i++)
+ RecordType.typeToClass(i); // will return null for some values
}
}
Deleted: labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLocking.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLocking.java 2010-02-12 08:49:08 UTC (rev 31604)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLocking.java 2010-02-12 13:11:46 UTC (rev 31605)
@@ -1,126 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags.
- * See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public License,
- * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- * (C) 2005-2006,
- * @author JBoss Inc.
- */
-package com.hp.mwtests.ts.arjuna.file;
-
-/*
- * Copyright (C) 1998, 1999, 2000,
- *
- * Arjuna Solutions Limited,
- * Newcastle upon Tyne,
- * Tyne and Wear,
- * UK.
- *
- * $Id: FileLocking.java 2342 2006-03-30 13:06:17Z $
- */
-
-import com.arjuna.ats.arjuna.utils.*;
-
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-import java.io.*;
-
-import java.io.IOException;
-import java.lang.InterruptedException;
-
-/*
- * A simple test of file locking. Create 2 threads and have them
- * contend to exclusively lock the same file.
- */
-
-class FileContender extends Thread
-{
-
- public FileContender(File file, int id, int lmode)
- {
- _theFile = file;
- _id = id;
- _mode = lmode;
- }
-
- public void run()
- {
- FileLock fileLock = new FileLock(_theFile);
-
- for (int i = 0; i < 100; i++) {
- if (fileLock.lock(_mode, false)) {
- System.out.println("thread " + _id + " locked file.");
-
- Thread.yield();
-
- fileLock.unlock();
-
- System.out.println("thread " + _id + " unlocked file.");
-
- Thread.yield();
- } else {
- System.out.println("thread " + _id + " could not lock file.");
-
- Thread.yield();
- }
- }
- }
-
- private File _theFile;
- private int _id;
- private int _mode;
-
-}
-
-public class FileLocking
-{
- @Test
- public void test() throws IOException
- {
- int lmode = FileLock.F_WRLCK;
-
- /*
- * Create the file to contend over.
- */
-
- File theFile = new File("foobar");
- theFile.deleteOnExit();
-
- DataOutputStream ofile = new DataOutputStream(new FileOutputStream(theFile));
-
- ofile.writeInt(0);
-
- /*
- * Now create the threads.
- */
-
- FileContender thread1 = new FileContender(theFile, 1, lmode);
- FileContender thread2 = new FileContender(theFile, 2, lmode);
-
- thread1.start();
- thread2.start();
-
- try {
- thread1.join();
- thread2.join();
- }
- catch (InterruptedException e) {
- fail("Test did not complete successfully.");
- }
- }
-
-
-}
Copied: labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLockingUnitTest.java (from rev 31358, labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLocking.java)
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLockingUnitTest.java (rev 0)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/file/FileLockingUnitTest.java 2010-02-12 13:11:46 UTC (rev 31605)
@@ -0,0 +1,141 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author JBoss Inc.
+ */
+package com.hp.mwtests.ts.arjuna.file;
+
+/*
+ * Copyright (C) 1998, 1999, 2000,
+ *
+ * Arjuna Solutions Limited,
+ * Newcastle upon Tyne,
+ * Tyne and Wear,
+ * UK.
+ *
+ * $Id: FileLocking.java 2342 2006-03-30 13:06:17Z $
+ */
+
+import com.arjuna.ats.arjuna.utils.*;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+import java.io.*;
+
+import java.io.IOException;
+import java.lang.InterruptedException;
+
+/*
+ * A simple test of file locking. Create 2 threads and have them
+ * contend to exclusively lock the same file.
+ */
+
+class FileContender extends Thread
+{
+
+ public FileContender(File file, int id, int lmode)
+ {
+ _theFile = file;
+ _id = id;
+ _mode = lmode;
+ }
+
+ public void run()
+ {
+ FileLock fileLock = new FileLock(_theFile);
+
+ for (int i = 0; i < 100; i++) {
+ if (fileLock.lock(_mode, false)) {
+ System.out.println("thread " + _id + " locked file.");
+
+ Thread.yield();
+
+ fileLock.unlock();
+
+ System.out.println("thread " + _id + " unlocked file.");
+
+ Thread.yield();
+ } else {
+ System.out.println("thread " + _id + " could not lock file.");
+
+ Thread.yield();
+ }
+ }
+ }
+
+ private File _theFile;
+ private int _id;
+ private int _mode;
+
+}
+
+public class FileLockingUnitTest
+{
+ @Test
+ public void test() throws IOException
+ {
+ int lmode = FileLock.F_WRLCK;
+
+ /*
+ * Create the file to contend over.
+ */
+
+ File theFile = new File("foobar");
+ theFile.deleteOnExit();
+
+ DataOutputStream ofile = new DataOutputStream(new FileOutputStream(theFile));
+
+ ofile.writeInt(0);
+
+ /*
+ * Now create the threads.
+ */
+
+ FileContender thread1 = new FileContender(theFile, 1, lmode);
+ FileContender thread2 = new FileContender(theFile, 2, lmode);
+
+ thread1.start();
+ thread2.start();
+
+ try {
+ thread1.join();
+ thread2.join();
+ }
+ catch (InterruptedException e) {
+ fail("Test did not complete successfully.");
+ }
+ }
+
+ @Test
+ public void testMultipleLock () throws Exception
+ {
+ FileLock fl = new FileLock(System.getProperty("java.io.tmpdir")+"/barfoo");
+
+ assertTrue(fl.lock(FileLock.F_RDLCK, true));
+ assertTrue(fl.lock(FileLock.F_RDLCK));
+
+ assertTrue(fl.unlock());
+ assertTrue(fl.unlock());
+
+ assertEquals(FileLock.modeString(FileLock.F_RDLCK), "FileLock.F_RDLCK");
+ assertEquals(FileLock.modeString(FileLock.F_WRLCK), "FileLock.F_WRLCK");
+ assertEquals(FileLock.modeString(-1), "Unknown");
+ }
+
+}
Added: labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/recovery/TransactionStatusConnectionManagerUnitTest.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/recovery/TransactionStatusConnectionManagerUnitTest.java (rev 0)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/recovery/TransactionStatusConnectionManagerUnitTest.java 2010-02-12 13:11:46 UTC (rev 31605)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author JBoss Inc.
+ */
+package com.hp.mwtests.ts.arjuna.recovery;
+
+import org.junit.Test;
+
+import com.arjuna.ats.arjuna.common.Uid;
+import com.arjuna.ats.arjuna.coordinator.ActionStatus;
+import com.arjuna.ats.arjuna.recovery.TransactionStatusConnectionManager;
+
+import static org.junit.Assert.*;
+
+/*
+ * This class does get tested mostly through other tests that we run. This test
+ * suite is simply to cover the edge cases.
+ */
+
+public class TransactionStatusConnectionManagerUnitTest
+{
+ @Test
+ public void testPresumedAbort ()
+ {
+ Uid tx = new Uid();
+ TransactionStatusConnectionManager tscm = new TransactionStatusConnectionManager();
+
+ assertEquals(tscm.getTransactionStatus(tx), ActionStatus.ABORTED);
+ }
+}
Modified: labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/state/IOStateUnitTest.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/state/IOStateUnitTest.java 2010-02-12 08:49:08 UTC (rev 31604)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/tests/classes/com/hp/mwtests/ts/arjuna/state/IOStateUnitTest.java 2010-02-12 13:11:46 UTC (rev 31605)
@@ -20,6 +20,9 @@
*/
package com.hp.mwtests.ts.arjuna.state;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
import org.junit.Test;
import com.arjuna.ats.arjuna.common.Uid;
@@ -34,8 +37,31 @@
public class IOStateUnitTest
{
@Test
- public void test() throws Exception
+ public void testIOObjectBuffer() throws Exception
{
+ PrintWriter pw = new PrintWriter(new StringWriter());
+ OutputBuffer obuff = new OutputBuffer(1024);
+
+ obuff.print(pw);
+
+ assertTrue(obuff.toString() != null);
+
+ OutputBuffer tobuff = new OutputBuffer(obuff);
+
+ assertTrue(tobuff.valid());
+
+ InputBuffer ibuff = new InputBuffer();
+
+ ibuff.print(pw);
+
+ InputBuffer tibuff = new InputBuffer(ibuff);
+
+ assertEquals(tibuff.valid(), false);
+ }
+
+ @Test
+ public void testIOObjectState() throws Exception
+ {
OutputObjectState oos = new OutputObjectState(new Uid(), "");
oos.packBoolean(true);
@@ -48,6 +74,12 @@
oos.packShort((short) 10);
oos.packString("test");
+ assertTrue(oos.valid());
+
+ PrintWriter pw = new PrintWriter(new StringWriter());
+
+ oos.print(pw);
+
assertTrue(oos.length() != 0);
assertTrue(oos.notempty());
assertTrue(oos.stateUid() != Uid.nullUid());
@@ -55,6 +87,10 @@
assertTrue(oos.size() > 0);
assertTrue(oos.type() != null);
+ OutputObjectState temp = new OutputObjectState(oos);
+
+ assertTrue(temp.toString() != null);
+
InputObjectState ios = new InputObjectState(oos);
assertTrue(ios.buffer() != null);
@@ -63,6 +99,14 @@
assertTrue(ios.size() > 0);
assertTrue(ios.stateUid() != Uid.nullUid());
+ assertTrue(ios.valid());
+
+ ios.print(pw);
+
+ InputObjectState is = new InputObjectState(ios);
+
+ assertTrue(is.toString() != null);
+
assertTrue(ios.unpackBoolean());
assertEquals(ios.unpackByte(), (byte) 0);
assertEquals(ios.unpackChar(), 'a');
More information about the jboss-svn-commits
mailing list