Package org.jugile.proto2.load

Source Code of org.jugile.proto2.load.DomainLockTest

package org.jugile.proto2.load;


import org.jugile.proto2.domain.*;
import org.jugile.util.Jugile;
import org.jugile.util.Timer;

public class DomainLockTest extends Thread {

  public static void main(String args[]) {
    Timer t = new Timer();
    t = new Timer();
    int numOfThreads = 40;
    for (int i = 0; i < numOfThreads; i++) {
      new DomainLockTest("th"+i, t).start();
    }
  }

  private Timer timer = null;
  public DomainLockTest(String str, Timer t) {
    super(str);
    timer = t;
  }
 
  public void run() {
    for (int i = 0; i < 1000; i++) {
      int task = (int)(Math.random() * 4);
      //d().startTx();
      try {
        if (task == 0) task0();
        if (task == 1) task1();
        if (task == 2) task2();
        if (task == 3) task3();
        //print(" doing commit");
        d().commit();
      } catch (Exception e) {
        print(" doing rollback");
        //e.printStackTrace();
        d().rollback();
      }
      sleepSome(10);
    }
    print("DONE! " + getName() + " ms: " + timer.stop());
  }

  private Domain d() {
    return Domain.getDomain();
  }
 
  private void task0() {
    for (int i = 0; i < 100; i++) {
      d().createFamily().setName("f"+i);
    }
    print("size: " + d().getFamilies().size());
  }
 
  private void task1() {
    for (Family b : d().getFamilies()) {
      String foo = b.getName();
      if ("fooo".equals(foo)) {
        print("found fooo");
        return;
      }
    }
  }
 
  private void task2() {
    int count = 0;
    for (Family b : d().getFamilies()) {
      if (++count%11 == 0) b.delete();
    }
  }
 
  private void task3() {
    for (int i = 0; i < 200; i++) {
      Family b = d().createFamily().setName("fail"+i);
    }
    Jugile.fail("foo");
  }
 
  public static void print(String msg) { System.out.println(msg); }
 
  private void sleepSome(int max) {
    try { sleep((int)(Math.random()*max));
    } catch (InterruptedException e) {Jugile.fail("interrupted"); }
  }
 
}
TOP

Related Classes of org.jugile.proto2.load.DomainLockTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.