func TestRunBroadcastFive(t *testing.T) { c := make(chan Packet, 100) sentinel := Packet{Addr: "sentinel"} var r run r.seqn = 1 r.out = c r.addr = []string{"v", "w", "x", "y", "z"} r.broadcast(newInvite(1)) c <- sentinel exp := msg{ Seqn: proto.Int64(1), Cmd: invite, Crnd: proto.Int64(1), } addrs := make([]string, len(r.addr)) for i := 0; i < len(r.addr); i++ { p := <-c addrs[i] = p.Addr var got msg err := proto.Unmarshal(p.Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) } assert.Equal(t, sentinel, <-c) assert.Equal(t, r.addr, addrs) }
func TestRunBroadcastThree(t *testing.T) { c := make(chan Packet, 100) sentinel := Packet{Addr: "sentinel"} var r run r.seqn = 1 r.out = c r.addrs = map[string]bool{ "x": true, "y": true, "z": true, } r.broadcast(newInvite(1)) c <- sentinel exp := msg{ Seqn: proto.Int64(1), Cmd: invite, Crnd: proto.Int64(1), } addrs := map[string]bool{} for i := 0; i < len(r.addrs); i++ { p := <-c addrs[p.Addr] = true var got msg err := proto.Unmarshal(p.Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) } assert.Equal(t, sentinel, <-c) assert.Equal(t, r.addrs, addrs) }
func TestRunBroadcastFive(t *testing.T) { c := make(chan Packet, 100) var r run r.seqn = 1 r.out = c r.addr = []*net.UDPAddr{ &net.UDPAddr{net.IP{1, 2, 3, 4}, 5}, &net.UDPAddr{net.IP{2, 3, 4, 5}, 6}, &net.UDPAddr{net.IP{3, 4, 5, 6}, 7}, &net.UDPAddr{net.IP{4, 5, 6, 7}, 8}, &net.UDPAddr{net.IP{5, 6, 7, 8}, 9}, } r.broadcast(newInvite(1)) c <- Packet{} exp := msg{ Seqn: proto.Int64(1), Cmd: invite, Crnd: proto.Int64(1), } addr := make([]*net.UDPAddr, len(r.addr)) for i := 0; i < len(r.addr); i++ { p := <-c addr[i] = p.Addr var got msg err := proto.Unmarshal(p.Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) } assert.Equal(t, Packet{}, <-c) assert.Equal(t, r.addr, addr) }
func TestRecvPacket(t *testing.T) { q := new(vector.Vector) recvPacket(q, Packet{"x", mustMarshal(&msg{Seqn: proto.Int64(1)})}) recvPacket(q, Packet{"x", mustMarshal(&msg{Seqn: proto.Int64(2)})}) recvPacket(q, Packet{"x", mustMarshal(&msg{Seqn: proto.Int64(3)})}) assert.Equal(t, 3, q.Len()) }
func (*MyCalcService) Divide(req *CalcRequest, resp *CalcResponse) (err os.Error) { resp.Result = proto.Int64(0) defer func() { if x := recover(); x != nil { if ex, ok := x.(os.Error); ok { err = ex } else { err = os.ErrorString(fmt.Sprint(x)) } } }() resp.Result = proto.Int64(proto.GetInt64(req.A) / proto.GetInt64(req.B)) resp.Remainder = proto.Int64(proto.GetInt64(req.A) % proto.GetInt64(req.B)) return }
// chasing a moving target with exec here. currently it's kind of // clumsy to pull out the return code. with luck the target will // move again in a way i like. func runPlugin(cmd, env []string, timeout int64) (result *CheckResult) { var output bytes.Buffer rc := 0 log.Printf("running check %s", cmd) /* c := exec.Command(cmd[0], cmd...)*/ c := exec.Command(cmd[0], cmd[1:]...) c.Stdout = &output starttime := time.Nanoseconds() err := c.Start() if err != nil { log.Fatal("Error running command ", cmd, ": ", err) } defer c.Process.Release() timer := time.AfterFunc(timeout, func() { c.Process.Kill() }) err = c.Wait() timer.Stop() endtime := time.Nanoseconds() /* log.Print(msg)*/ if err != nil { if msg, ok := err.(*os.Waitmsg); ok { rc = msg.ExitStatus() } else { log.Print("Error running command ", cmd, ": ", err) } } result = &CheckResult{ StartTimestamp: proto.Int64(starttime), EndTimestamp: proto.Int64(endtime), Status: NewCheckStatus(CheckStatus(rc)), CheckPassive: proto.Bool(*flagPassive), } switch rc { case 0, 1, 2, 3: // this is ok! log.Printf("%s: returned %s", cmd, CheckStatus_name[int32(rc)]) result.Status = NewCheckStatus(CheckStatus(rc)) result.CheckOutput = proto.String(string(bytes.TrimSpace(output.Bytes()))) break default: // XXX check for timeout/sig9, presently assumed log.Printf("%s: return code %d", cmd, rc) result.Status = NewCheckStatus(CheckStatus_UNKNOWN) result.CheckOutput = proto.String(fmt.Sprintf("UNKNOWN: Command timed out after %d seconds\n", *flagCmdTimeout) + string(bytes.TrimSpace(output.Bytes()))) } return result }
func NewWavelet(url *WaveUrl) (result *Wavelet) { result = &Wavelet{Url: url} result.HashedVersion.HistoryHash = []byte(url.String()) result.HashedVersion.Version = proto.Int64(0) result.Documents = make(map[string]*WaveletDocument) return result }
func TestManagerTickQueue(t *testing.T) { ticker := make(chan int64) st := store.New() defer close(st.Ops) in := make(chan Packet) cfg := &Config{ Alpha: 1, Store: st, In: in, Ticker: ticker, Out: make(chan Packet, 100), } m := NewManager(cfg) st.Ops <- store.Op{ Seqn: 1, Mut: store.MustEncodeSet(node+"/a/addr", "x", 0), } for (<-m.Stats).Runs < 1 { } // get it to tick for seqn 2 in <- Packet{Data: mustMarshal(&msg{Seqn: proto.Int64(2), Cmd: propose})} assert.Equal(t, 1, (<-m.Stats).WaitTicks) ticker <- time.Nanoseconds() + initialWaitBound*2 assert.Equal(t, int64(1), (<-m.Stats).TotalTicks) }
func TestManagerDeletesSuccessfulRun(t *testing.T) { st := store.New() defer close(st.Ops) in := make(chan Packet) out := make(chan Packet, 100) cfg := &Config{ Alpha: 1, Store: st, In: in, Out: out, Ops: st.Ops, } m := NewManager(cfg) st.Ops <- store.Op{ Seqn: 1, Mut: store.MustEncodeSet(node+"/a/addr", "x", 0), } for (<-m.Stats).TotalRuns < 1 { } in <- Packet{ Data: mustMarshal(&msg{Seqn: proto.Int64(2), Cmd: learn, Value: []byte("foo")}), Addr: "127.0.0.1:9999", } for (<-m.Stats).TotalRuns < 2 { } assert.Equal(t, 1, (<-m.Stats).Runs) }
// fetch fetches readBufferSize bytes starting at the given offset. On success, // the data is saved as r.buf. func (r *reader) fetch(off int64) error { req := &pb.FetchDataRequest{ BlobKey: proto.String(string(r.blobKey)), StartIndex: proto.Int64(off), EndIndex: proto.Int64(off + readBufferSize - 1), // EndIndex is inclusive. } res := &pb.FetchDataResponse{} if err := r.c.Call("blobstore", "FetchData", req, res, nil); err != nil { return err } if len(res.Data) == 0 { return io.EOF } r.buf, r.r, r.off = res.Data, 0, off return nil }
// UploadURL creates an upload URL for the form that the user will // fill out, passing the application path to load when the POST of the // form is completed. These URLs expire and should not be reused. The // opts parameter may be nil. func UploadURL(c appengine.Context, successPath string, opts *UploadURLOptions) (*url.URL, error) { req := &pb.CreateUploadURLRequest{ SuccessPath: proto.String(successPath), } if opts != nil { if opts.MaxUploadBytes != 0 { req.MaxUploadSizeBytes = proto.Int64(opts.MaxUploadBytes) } if opts.MaxUploadBytesPerBlob != 0 { req.MaxUploadSizePerBlobBytes = proto.Int64(opts.MaxUploadBytesPerBlob) } } res := &pb.CreateUploadURLResponse{} if err := c.Call("blobstore", "CreateUploadURL", req, res, nil); err != nil { return nil, err } return url.Parse(*res.Url) }
func TestManagerPacketQueue(t *testing.T) { in := make(chan Packet) st := store.New() out := make(chan Packet, 100) m := newManager("", 0, nil, in, nil, nil, nil, 0, st, out) in <- Packet{"x", mustMarshal(&msg{Seqn: proto.Int64(1)})} assert.Equal(t, 1, (<-m).WaitPackets) }
func TestRunVoteDelivered(t *testing.T) { r := run{} r.out = make(chan Packet, 100) r.ops = make(chan store.Op, 100) r.l.init(1) p := packet{ msg: msg{ Seqn: proto.Int64(1), Cmd: vote, Vrnd: proto.Int64(1), Value: []byte("foo"), }, Addr: "X", } r.update(p, new(vector.Vector)) assert.Equal(t, true, r.l.done) assert.Equal(t, "foo", r.l.v) }
func TestRunSendsAcceptorPacket(t *testing.T) { c := make(chan Packet, 100) var r run r.out = c r.addr = []string{"x", "y"} var got msg exp := msg{ Seqn: proto.Int64(0), Cmd: rsvp, Crnd: proto.Int64(1), Vrnd: proto.Int64(0), } r.update(packet{msg: *newInviteSeqn1(1)}, new(vector.Vector)) <-c err := proto.Unmarshal((<-c).Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) assert.Equal(t, 0, len(c)) }
func TestRunSendsCoordPacket(t *testing.T) { c := make(chan Packet, 100) var r run r.c.crnd = 1 r.out = c r.addr = []string{"x", "y"} var got msg exp := msg{ Seqn: proto.Int64(0), Cmd: invite, Crnd: proto.Int64(1), } r.update(packet{msg: *newPropose("foo")}, new(vector.Vector)) <-c err := proto.Unmarshal((<-c).Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) assert.Equal(t, 0, len(c)) }
func newTestMessage() *pb.MyMessage { msg := &pb.MyMessage{ Count: proto.Int32(42), Name: proto.String("Dave"), Quote: proto.String(`"I didn't want to go."`), Pet: []string{"bunny", "kitty", "horsey"}, Inner: &pb.InnerMessage{ Host: proto.String("footrest.syd"), Port: proto.Int32(7001), Connected: proto.Bool(true), }, Others: []*pb.OtherMessage{ &pb.OtherMessage{ Key: proto.Int64(0xdeadbeef), Value: []byte{1, 65, 7, 12}, }, &pb.OtherMessage{ Weight: proto.Float32(6.022), Inner: &pb.InnerMessage{ Host: proto.String("lesha.mtv"), Port: proto.Int32(8002), }, }, }, Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE), Somegroup: &pb.MyMessage_SomeGroup{ GroupField: proto.Int32(8), }, // One normally wouldn't do this. // This is an undeclared tag 13, as a varint (wire type 0) with value 4. XXX_unrecognized: []byte{13<<3 | 0, 4}, } ext := &pb.Ext{ Data: proto.String("Big gobs for big rats"), } if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil { panic(err) } // Add an unknown extension. We marshal a pb.Ext, and fake the ID. b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")}) if err != nil { panic(err) } b = append(proto.EncodeVarint(104<<3|proto.WireBytes), b...) proto.SetRawExtension(msg, 104, b) // Extensions can be plain fields, too, so let's test that. b = append(proto.EncodeVarint(105<<3|proto.WireVarint), 19) proto.SetRawExtension(msg, 105, b) return msg }
func TestRunVoteDelivered(t *testing.T) { r := run{} r.out = make(chan Packet, 100) r.ops = make(chan store.Op, 100) r.l.init(1, 1) p := packet{ msg: msg{ Seqn: proto.Int64(1), Cmd: vote, Vrnd: proto.Int64(1), Value: []byte("foo"), }, Addr: &net.UDPAddr{net.IP{1, 2, 3, 4}, 5}, } r.update(&p, 0, new(triggers)) assert.Equal(t, true, r.l.done) assert.Equal(t, "foo", r.l.v) }
func (self *WaveletHistory) Range(startVersion int64, startHash []byte, endVersion int64, endHash []byte, limit int64) (result *ProtocolWaveletHistory, err os.Error) { // Error checking if startVersion < 0 || startVersion == int64(len(self.history)) { return nil, os.NewError("Invalid version number") } if endVersion < 1 || endVersion > int64(len(self.history)) { return nil, os.NewError("Invalid version number") } if endVersion <= startVersion { return nil, os.NewError("Invalid version number") } s := &ProtocolHashedVersion{Version:proto.Int64(startVersion), HistoryHash:startHash} e := &ProtocolHashedVersion{Version:proto.Int64(endVersion), HistoryHash:endHash} // Get the history mutations := self.history[startVersion:endVersion] if mutations[0] == nil || !mutations[0].HashedVersion.Equals(s) { return nil, os.NewError("Invalid hash or version") } if mutations[len(mutations)-1] == nil || mutations[len(mutations)-1].HistoryHash.Equals(e) { return nil, os.NewError("Invalid hash or version") } // Remove all items which are nil list := make([]*ProtocolWaveletDelta, 0, endVersion - startVersion) pos := 0 for _, p := range mutations { if p == nil { continue } list[pos] = p pos++ } // TODO: Implement the limit result = &ProtocolWaveletHistory{} result.Deltas := list[:] return result, nil }
// valueToProto converts a named value to a newly allocated Property. // The returned error string is empty on success. func valueToProto(defaultAppID, name string, v reflect.Value, multiple bool) (p *pb.Property, errStr string) { var ( pv pb.PropertyValue unsupported bool ) switch v.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: pv.Int64Value = proto.Int64(v.Int()) case reflect.Bool: pv.BooleanValue = proto.Bool(v.Bool()) case reflect.String: pv.StringValue = proto.String(v.String()) case reflect.Float32, reflect.Float64: pv.DoubleValue = proto.Float64(v.Float()) case reflect.Ptr: if k, ok := v.Interface().(*Key); ok { if k == nil { return nil, nilKeyErrStr } pv.Referencevalue = keyToReferenceValue(defaultAppID, k) } else { unsupported = true } case reflect.Slice: if b, ok := v.Interface().([]byte); ok { pv.StringValue = proto.String(string(b)) } else { // nvToProto should already catch slice values. // If we get here, we have a slice of slice values. unsupported = true } default: unsupported = true } if unsupported { return nil, "unsupported datastore value type: " + v.Type().String() } p = &pb.Property{ Name: proto.String(name), Value: &pv, Multiple: proto.Bool(multiple), } switch v.Interface().(type) { case []byte: p.Meaning = pb.NewProperty_Meaning(pb.Property_BLOB) case appengine.BlobKey: p.Meaning = pb.NewProperty_Meaning(pb.Property_BLOBKEY) case Time: p.Meaning = pb.NewProperty_Meaning(pb.Property_GD_WHEN) } return p, "" }
func PrintLocationsAsProto(locations map[string]Location) []byte { locProto := &locationProto.LocationInfo{ make([]*locationProto.Location, len(locations)), nil} loc := locProto.Location for name, location := range locations { loc[0] = &locationProto.Location{ proto.String(name), proto.Float64(float64(location.lat)), proto.Float64(float64(location.lng)), nil, proto.Float64(float64(location.accuracy)), proto.Int64(location.timestamp), nil} loc = loc[1:] } data, _ := proto.Marshal(locProto) return data }
func TestRunSendsAcceptorPacket(t *testing.T) { c := make(chan Packet, 100) x := &net.UDPAddr{net.IP{1, 2, 3, 4}, 5} y := &net.UDPAddr{net.IP{2, 3, 4, 5}, 6} var r run r.out = c r.addr = []*net.UDPAddr{x, y} var got msg exp := msg{ Seqn: proto.Int64(0), Cmd: rsvp, Crnd: proto.Int64(1), Vrnd: proto.Int64(0), } r.update(&packet{msg: *newInviteSeqn1(1)}, 0, new(vector.Vector)) <-c err := proto.Unmarshal((<-c).Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) assert.Equal(t, 0, len(c)) }
func TestRunReturnTrueIfLearned(t *testing.T) { r := run{} r.out = make(chan Packet, 100) r.ops = make(chan store.Op, 100) p := packet{msg: msg{ Seqn: proto.Int64(1), Cmd: learn, Value: []byte("foo"), }} r.update(&p, 0, new(vector.Vector)) assert.T(t, r.l.done) }
// Add adds the task to a named queue. // An empty queue name means that the default queue will be used. // Add returns an equivalent Task with defaults filled in, including setting // the task's Name field to the chosen name if the original was empty. func Add(c appengine.Context, task *Task, queueName string) (*Task, error) { if queueName == "" { queueName = "default" } req := &taskqueue_proto.TaskQueueAddRequest{ QueueName: []byte(queueName), TaskName: []byte(task.Name), EtaUsec: proto.Int64(time.Nanoseconds()/1e3 + task.Delay), } method := task.Method if method == "" { method = "POST" } if method == "PULL" { // Pull-based task req.Body = task.Payload req.Mode = taskqueue_proto.NewTaskQueueMode_Mode(taskqueue_proto.TaskQueueMode_PULL) // TODO: More fields will need to be set. } else { // HTTP-based task if v, ok := taskqueue_proto.TaskQueueAddRequest_RequestMethod_value[method]; ok { req.Method = taskqueue_proto.NewTaskQueueAddRequest_RequestMethod( taskqueue_proto.TaskQueueAddRequest_RequestMethod(v)) } else { return nil, fmt.Errorf("taskqueue: bad method %q", method) } req.Url = []byte(task.Path) for k, vs := range task.Header { for _, v := range vs { req.Header = append(req.Header, &taskqueue_proto.TaskQueueAddRequest_Header{ Key: []byte(k), Value: []byte(v), }) } } if method == "POST" || method == "PUT" { req.Body = task.Payload } } res := &taskqueue_proto.TaskQueueAddResponse{} if err := c.Call("taskqueue", "Add", req, res, nil); err != nil { return nil, err } resultTask := *task resultTask.Method = method if task.Name == "" { resultTask.Name = string(res.ChosenTaskName) } return &resultTask, nil }
func TestManagerPumpDropsOldPackets(t *testing.T) { st := store.New() defer close(st.Ops) st.Ops <- store.Op{1, store.MustEncodeSet(node+"/a/addr", "x", 0)} st.Ops <- store.Op{2, store.MustEncodeSet("/ctl/cal/0", "a", 0)} var m Manager m.run = make(map[int64]*run) m.event(<-mustWait(st, 2)) m.pump() recvPacket(&m.packet, Packet{"x", mustMarshal(&msg{Seqn: proto.Int64(1)})}) m.pump() assert.Equal(t, 0, m.Stats.WaitPackets) }
func TestRunSendsCoordPacket(t *testing.T) { c := make(chan Packet, 100) x := &net.UDPAddr{net.IP{1, 2, 3, 4}, 5} y := &net.UDPAddr{net.IP{2, 3, 4, 5}, 6} var r run r.c.crnd = 1 r.out = c r.addr = []*net.UDPAddr{x, y} var got msg exp := msg{ Seqn: proto.Int64(0), Cmd: invite, Crnd: proto.Int64(1), } r.update(&packet{msg: *newPropose("foo")}, -1, new(triggers)) <-c err := proto.Unmarshal((<-c).Data, &got) assert.Equal(t, nil, err) assert.Equal(t, exp, got) assert.Equal(t, 0, len(c)) }
func TestRunReturnFalseIfNotLearned(t *testing.T) { r := run{} r.out = make(chan Packet, 100) r.ops = make(chan store.Op, 100) p := packet{msg: msg{ Seqn: proto.Int64(1), Cmd: invite, Value: []byte("foo"), }} r.update(&p, 0, new(triggers)) assert.T(t, !r.l.done) }
func TestApplyTriggers(t *testing.T) { packets := new(vector.Vector) triggers := new(vector.Vector) heap.Push(triggers, trigger{t: 1, n: 1}) heap.Push(triggers, trigger{t: 2, n: 2}) heap.Push(triggers, trigger{t: 3, n: 3}) heap.Push(triggers, trigger{t: 4, n: 4}) heap.Push(triggers, trigger{t: 5, n: 5}) heap.Push(triggers, trigger{t: 6, n: 6}) heap.Push(triggers, trigger{t: 7, n: 7}) heap.Push(triggers, trigger{t: 8, n: 8}) heap.Push(triggers, trigger{t: 9, n: 9}) n := applyTriggers(packets, triggers, 5, &msg{Cmd: tick}) assert.Equal(t, 5, n) expTriggers := new(vector.Vector) expPackets := new(vector.Vector) heap.Push(expPackets, &packet{msg: msg{Cmd: tick, Seqn: proto.Int64(1)}}) heap.Push(expPackets, &packet{msg: msg{Cmd: tick, Seqn: proto.Int64(2)}}) heap.Push(expPackets, &packet{msg: msg{Cmd: tick, Seqn: proto.Int64(3)}}) heap.Push(expPackets, &packet{msg: msg{Cmd: tick, Seqn: proto.Int64(4)}}) heap.Push(expPackets, &packet{msg: msg{Cmd: tick, Seqn: proto.Int64(5)}}) heap.Push(expTriggers, trigger{t: 6, n: 6}) heap.Push(expTriggers, trigger{t: 7, n: 7}) heap.Push(expTriggers, trigger{t: 8, n: 8}) heap.Push(expTriggers, trigger{t: 9, n: 9}) sort.Sort(packets) sort.Sort(triggers) sort.Sort(expPackets) sort.Sort(expTriggers) assert.Equal(t, expTriggers, triggers) assert.Equal(t, expPackets, packets) }
func TestManagerDropsOldPackets(t *testing.T) { runs := make(chan *run) defer close(runs) st := store.New() in := make(chan Packet) out := make(chan Packet, 100) m := newManager("", 0, nil, in, runs, nil, nil, 0, st, out) run := run{seqn: 2, ops: make(chan store.Op, 100)} runs <- &run in <- Packet{"x", mustMarshal(&msg{Seqn: proto.Int64(1)})} assert.Equal(t, 0, (<-m).WaitPackets) }
func TestRunReturnFalseIfNotLearned(t *testing.T) { r := run{} r.out = make(chan Packet, 100) r.ops = make(chan store.Op, 100) p := packet{ msg: msg{ Seqn: proto.Int64(1), Cmd: invite, Value: []byte("foo"), }, Addr: "X", } learned := r.update(p, new(vector.Vector)) assert.T(t, !learned) }
func createSubmit(version int64, hash []byte, doc string, docop *ProtocolDocumentOperation) []byte { submit := &ClientSubmitRequest{} delta := &ProtocolWaveletDelta{} delta.Author = proto.String("torben") delta.HashedVersion = &ProtocolHashedVersion{Version: proto.Int64(version), HistoryHash: hash} mut := &ProtocolWaveletOperation_MutateDocument{DocumentId: proto.String(doc), DocumentOperation: docop} op := &ProtocolWaveletOperation{MutateDocument: mut} delta.Operation = [...]*ProtocolWaveletOperation{op}[:] submit.Delta = delta var buffer bytes.Buffer Marshal(submit, &buffer) log.Println(buffer.String()) return buffer.Bytes() }