func NewTestCase(t *testing.T) *testCase { me := new(testCase) me.tester = t me.secret = RandomBytes(20) me.tmp, _ = ioutil.TempDir("", "") workerTmp := me.tmp + "/worker-tmp" os.Mkdir(workerTmp, 0700) opts := WorkerOptions{ Secret: me.secret, TempDir: workerTmp, CacheDir: me.tmp + "/worker-cache", Jobs: 1, } me.worker = NewWorkerDaemon(&opts) // TODO - pick unused port me.coordinatorPort = int(rand.Int31n(60000) + 1024) me.workerPort = int(rand.Int31n(60000) + 1024) coordinatorAddr := fmt.Sprintf(":%d", me.coordinatorPort) var wg sync.WaitGroup wg.Add(2) go func() { me.coordinator = NewCoordinator(me.secret) go me.coordinator.PeriodicCheck() go me.coordinator.ServeHTTP(me.coordinatorPort) wg.Done() }() go func() { masterCache := NewContentCache(me.tmp + "/master-cache") me.master = NewMaster( masterCache, coordinatorAddr, []string{}, me.secret, []string{}, 1) me.master.SetKeepAlive(1.0) me.socket = me.tmp + "/master-socket" go me.master.Start(me.socket) wg.Done() }() go me.worker.RunWorkerServer(me.workerPort, coordinatorAddr) wg.Wait() for me.coordinator.WorkerCount() == 0 { log.Println("reporting...") me.worker.report(coordinatorAddr, me.workerPort) } wd := me.tmp + "/wd" os.MkdirAll(wd, 0755) return me }
func RandomBytes(n int) []byte { c := make([]byte, 0) for i := 0; i < n; i++ { c = append(c, byte(rand.Int31n(256))) } return c }
func AuthenticateClient(conn net.Conn, secret []byte) os.Error { challenge := make([]byte, 0) for i := 0; i < challengeLength; i++ { challenge = append(challenge, byte(rand.Int31n(256))) } _, err := conn.Write(challenge) if err != nil { return err } h := hmac.NewSHA1(secret) _, err = h.Write(challenge) expected := h.Sum() response := make([]byte, len(expected)) n, err := conn.Read(response) if err != nil { return err } response = response[:n] if bytes.Compare(response, expected) != 0 { log.Println("Authentication failure from", conn.RemoteAddr()) return os.NewError("Mismatch in response") } conn.Write([]byte("OK")) return nil }
func randomPlusMinus() (val int) { switch rand.Int31n(3) { case 0: val = 0 case 1: val = 1 case 2: val = -1 } return }
/* return a random particle from p that is not in e */ func randParticle(p Particles, e Particles) (r *Particle) { contains := true for contains { r = p[rand.Int31n(int32(len(p)))] contains = false for i := 0; e != nil && i < len(e); i++ { if e[i] == r { contains = true } } } return }
func TestAuthenticate(t *testing.T) { secret := RandomBytes(20) port := int(rand.Int31n(60000) + 1024) out := make(chan net.Conn) go SetupServer(int(port), secret, out) time.Sleep(1e9) hostname, _ := os.Hostname() addr := fmt.Sprintf("%s:%d", hostname, port) _, err := DialTypedConnection(addr, RPC_CHANNEL, secret) if err != nil { t.Fatal("unexpected failure", err) } <-out c, err := DialTypedConnection(addr, RPC_CHANNEL, []byte("foobar")) if c != nil { t.Error("expect failure") } }
/* Query1, DML(INSERT) on role_info. */ func queryRoleInfo1(cli *mysql.Client) { query_name := "[RoleInfo.query1]" loop_count := 1000 type row struct { // PK: (userid, roleid, server, date) userid int32 roleid int32 server int8 date string name string } fmt.Printf("%s started...\n", query_name) starttime := time.Nanoseconds() for i := 0; i < loop_count; i++ { newrow := &row{} newrow.userid = rand.Int31n(100000) newrow.roleid = rand.Int31n(10000) newrow.server = int8(rand.Intn(100)) newrow.date = "2011-06-26 10:20:24" newrow.name = "testname" if VERBOSE_MODE { fmt.Printf("%s INSERT IGNORE INTO role_info "+ "VALUES(%d, %d, %d, %q, %q)\n", query_name, newrow.userid, newrow.roleid, newrow.server, newrow.date, newrow.name) } else { if i == 0 { fmt.Printf("%s INSERT IGNORE INTO role_info "+ "VALUES(%d, %d, %d, %q, %q)\n", query_name, newrow.userid, newrow.roleid, newrow.server, newrow.date, newrow.name) fmt.Printf("%s ...(%d lines)\n", query_name, loop_count-1) } } if !DRYRUN_MODE { cli.Start() stmt, err := cli.Prepare("INSERT IGNORE INTO role_info " + "VALUES(?,?,?,?,?)") if err != nil { fmt.Fprintln(os.Stderr, query_name+err.String()) os.Exit(1) } err = stmt.BindParams(newrow.userid, newrow.roleid, newrow.server, newrow.date, newrow.name) if err != nil { fmt.Fprintln(os.Stderr, query_name+err.String()) os.Exit(1) } err = stmt.Execute() if err != nil { fmt.Fprintln(os.Stderr, query_name+err.String()) os.Exit(1) } cli.Rollback() } } endtime := time.Nanoseconds() if !DRYRUN_MODE { fmt.Printf("%s ended. Averange query time: %d nanosecs.\n", query_name, (endtime-starttime)/((int64)(loop_count))) } else { fmt.Printf("%s ended.\n", query_name) } }
func nagomi_bot(nagomin vector.StringVector) vector.StringVector { rand.Seed(time.Nanoseconds()) // client情報を作成 c := twitter.ClientBuilder(BOT_NAME, PASSWORD) // Timelineを取得 tweets := c.HomeTimeline(0, 0, 0, 0) //fmt.Printf("tweet = %v\n",tweets) // botへのリプライを取得 direct_messages := c.DirectMessages(0, 0, 0, 0) //DMの内容をデータベースに登録する処理の開始 for i := 0; i < len(direct_messages); i++ { f, _ := file.Open("nagomin.txt", os.O_WRONLY|os.O_APPEND, 644) bufy := bufio.NewWriter(f) //DMの内容がすでにデータベースに登録されてないかチェック flag := 0 for j := 0; j < len(nagomin); j++ { //登録されている場合の処理 if string(nagomin[j]) == direct_messages[i].Text { flag = 1 break } } //データベースに新規登録処理 if flag == 0 { fmt.Printf("%v\n", direct_messages[i].Text) bufy.WriteString(direct_messages[i].Text + "\n") //データベースに追加 bufy.Flush() //すでに読み込んでいるデータベースを更新 nagomin.Push(direct_messages[i].Text) } f.Close() } //現在の時刻の取得 current_t := time.LocalTime() tweet := tweets[0] //最新ツイートの時刻を取得 last_tweet_t, _ := time.Parse(time.RubyDate, tweet.Created_at) timespan := (current_t.Seconds() - last_tweet_t.Seconds()) //ツイートする最低間隔を設定(botが連続でツイートすることを防止する) if timespan < MIN_TIME { return nagomin } //APIからのつぶやきを最新ツイートに含めないようにする処理 i := 0 for i = 0; tweets[i].Source == "<a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>"; i++ { } tweet = tweets[i] last_tweet_t, _ = time.Parse(time.RubyDate, tweet.Created_at) //現在時刻と最新ツイートの時刻との差分を取得 timespan = (current_t.Seconds() - last_tweet_t.Seconds()) //ツイート処理 if timespan > TWEET_TIME { str := nagomin[rand.Int31n(int32(len(nagomin)))] fmt.Printf("%d秒経過したので,ツイートします.\n", timespan) c.Post(fmt.Sprintf("【nagomin】%s", str)) } return nagomin }
func NewCellState() *CellState { return &CellState{rand.Int31n(6)} }