diff options
Diffstat (limited to 'pkg/eventchannel')
-rw-r--r-- | pkg/eventchannel/BUILD | 39 | ||||
-rw-r--r-- | pkg/eventchannel/event.proto | 27 | ||||
-rw-r--r-- | pkg/eventchannel/event_test.go | 146 | ||||
-rw-r--r-- | pkg/eventchannel/eventchannel_go_proto/event.pb.go | 156 | ||||
-rw-r--r-- | pkg/eventchannel/eventchannel_state_autogen.go | 3 |
5 files changed, 159 insertions, 212 deletions
diff --git a/pkg/eventchannel/BUILD b/pkg/eventchannel/BUILD deleted file mode 100644 index a493e3407..000000000 --- a/pkg/eventchannel/BUILD +++ /dev/null @@ -1,39 +0,0 @@ -load("//tools:defs.bzl", "go_library", "go_test", "proto_library") - -package(licenses = ["notice"]) - -go_library( - name = "eventchannel", - srcs = [ - "event.go", - "event_any.go", - "rate.go", - ], - visibility = ["//:sandbox"], - deps = [ - ":eventchannel_go_proto", - "//pkg/log", - "//pkg/sync", - "//pkg/unet", - "@org_golang_google_protobuf//encoding/prototext:go_default_library", - "@org_golang_google_protobuf//proto:go_default_library", - "@org_golang_google_protobuf//types/known/anypb:go_default_library", - "@org_golang_x_time//rate:go_default_library", - ], -) - -proto_library( - name = "eventchannel", - srcs = ["event.proto"], - visibility = ["//:sandbox"], -) - -go_test( - name = "eventchannel_test", - srcs = ["event_test.go"], - library = ":eventchannel", - deps = [ - "//pkg/sync", - "@org_golang_google_protobuf//proto:go_default_library", - ], -) diff --git a/pkg/eventchannel/event.proto b/pkg/eventchannel/event.proto deleted file mode 100644 index 4b24ac47c..000000000 --- a/pkg/eventchannel/event.proto +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -syntax = "proto3"; - -package gvisor; - -// DebugEvent encapsulates any other event protobuf in text format. This is -// useful because clients reading events emitted this way do not need to link -// the event protobufs to display them in a human-readable format. -message DebugEvent { - // Name of the inner message. - string name = 1; - // Text representation of the inner message content. - string text = 2; -} diff --git a/pkg/eventchannel/event_test.go b/pkg/eventchannel/event_test.go deleted file mode 100644 index 0dd408f76..000000000 --- a/pkg/eventchannel/event_test.go +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright 2019 The gVisor Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package eventchannel - -import ( - "fmt" - "testing" - "time" - - "google.golang.org/protobuf/proto" - "gvisor.dev/gvisor/pkg/sync" -) - -// testEmitter is an emitter that can be used in tests. It records all events -// emitted, and whether it has been closed. -type testEmitter struct { - // mu protects all fields below. - mu sync.Mutex - - // events contains all emitted events. - events []proto.Message - - // closed records whether Close() was called. - closed bool -} - -// Emit implements Emitter.Emit. -func (te *testEmitter) Emit(msg proto.Message) (bool, error) { - te.mu.Lock() - defer te.mu.Unlock() - te.events = append(te.events, msg) - return false, nil -} - -// Close implements Emitter.Close. -func (te *testEmitter) Close() error { - te.mu.Lock() - defer te.mu.Unlock() - if te.closed { - return fmt.Errorf("closed called twice") - } - te.closed = true - return nil -} - -// testMessage implements proto.Message for testing. -type testMessage struct { - proto.Message - - // name is the name of the message, used by tests to compare messages. - name string -} - -func TestMultiEmitter(t *testing.T) { - // Create three testEmitters, tied together in a multiEmitter. - me := &multiEmitter{} - var emitters []*testEmitter - for i := 0; i < 3; i++ { - te := &testEmitter{} - emitters = append(emitters, te) - me.AddEmitter(te) - } - - // Emit three messages to multiEmitter. - names := []string{"foo", "bar", "baz"} - for _, name := range names { - m := testMessage{name: name} - if _, err := me.Emit(m); err != nil { - t.Fatalf("me.Emit(%v) failed: %v", m, err) - } - } - - // All three emitters should have all three events. - for _, te := range emitters { - if got, want := len(te.events), len(names); got != want { - t.Fatalf("emitter got %d events, want %d", got, want) - } - for i, name := range names { - if got := te.events[i].(testMessage).name; got != name { - t.Errorf("emitter got message with name %q, want %q", got, name) - } - } - } - - // Close multiEmitter. - if err := me.Close(); err != nil { - t.Fatalf("me.Close() failed: %v", err) - } - - // All testEmitters should be closed. - for _, te := range emitters { - if !te.closed { - t.Errorf("te.closed got false, want true") - } - } -} - -func TestRateLimitedEmitter(t *testing.T) { - // Create a RateLimittedEmitter that wraps a testEmitter. - te := &testEmitter{} - max := float64(5) // events per second - burst := 10 // events - rle := RateLimitedEmitterFrom(te, max, burst) - - // Send 50 messages in one shot. - for i := 0; i < 50; i++ { - if _, err := rle.Emit(testMessage{}); err != nil { - t.Fatalf("rle.Emit failed: %v", err) - } - } - - // We should have received only 10 messages. - if got, want := len(te.events), 10; got != want { - t.Errorf("got %d events, want %d", got, want) - } - - // Sleep for a second and then send another 50. - time.Sleep(1 * time.Second) - for i := 0; i < 50; i++ { - if _, err := rle.Emit(testMessage{}); err != nil { - t.Fatalf("rle.Emit failed: %v", err) - } - } - - // We should have at least 5 more message, plus maybe a few more if the - // test ran slowly. - got, wantAtLeast, wantAtMost := len(te.events), 15, 20 - if got < wantAtLeast { - t.Errorf("got %d events, want at least %d", got, wantAtLeast) - } - if got > wantAtMost { - t.Errorf("got %d events, want at most %d", got, wantAtMost) - } -} diff --git a/pkg/eventchannel/eventchannel_go_proto/event.pb.go b/pkg/eventchannel/eventchannel_go_proto/event.pb.go new file mode 100644 index 000000000..1d0812479 --- /dev/null +++ b/pkg/eventchannel/eventchannel_go_proto/event.pb.go @@ -0,0 +1,156 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.25.0 +// protoc v3.13.0 +// source: pkg/eventchannel/event.proto + +package gvisor + +import ( + proto "github.com/golang/protobuf/proto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// This is a compile-time assertion that a sufficiently up-to-date version +// of the legacy proto package is being used. +const _ = proto.ProtoPackageIsVersion4 + +type DebugEvent struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Text string `protobuf:"bytes,2,opt,name=text,proto3" json:"text,omitempty"` +} + +func (x *DebugEvent) Reset() { + *x = DebugEvent{} + if protoimpl.UnsafeEnabled { + mi := &file_pkg_eventchannel_event_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DebugEvent) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DebugEvent) ProtoMessage() {} + +func (x *DebugEvent) ProtoReflect() protoreflect.Message { + mi := &file_pkg_eventchannel_event_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DebugEvent.ProtoReflect.Descriptor instead. +func (*DebugEvent) Descriptor() ([]byte, []int) { + return file_pkg_eventchannel_event_proto_rawDescGZIP(), []int{0} +} + +func (x *DebugEvent) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *DebugEvent) GetText() string { + if x != nil { + return x.Text + } + return "" +} + +var File_pkg_eventchannel_event_proto protoreflect.FileDescriptor + +var file_pkg_eventchannel_event_proto_rawDesc = []byte{ + 0x0a, 0x1c, 0x70, 0x6b, 0x67, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x63, 0x68, 0x61, 0x6e, 0x6e, + 0x65, 0x6c, 0x2f, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, + 0x67, 0x76, 0x69, 0x73, 0x6f, 0x72, 0x22, 0x34, 0x0a, 0x0a, 0x44, 0x65, 0x62, 0x75, 0x67, 0x45, + 0x76, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_pkg_eventchannel_event_proto_rawDescOnce sync.Once + file_pkg_eventchannel_event_proto_rawDescData = file_pkg_eventchannel_event_proto_rawDesc +) + +func file_pkg_eventchannel_event_proto_rawDescGZIP() []byte { + file_pkg_eventchannel_event_proto_rawDescOnce.Do(func() { + file_pkg_eventchannel_event_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_eventchannel_event_proto_rawDescData) + }) + return file_pkg_eventchannel_event_proto_rawDescData +} + +var file_pkg_eventchannel_event_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_pkg_eventchannel_event_proto_goTypes = []interface{}{ + (*DebugEvent)(nil), // 0: gvisor.DebugEvent +} +var file_pkg_eventchannel_event_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_pkg_eventchannel_event_proto_init() } +func file_pkg_eventchannel_event_proto_init() { + if File_pkg_eventchannel_event_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_pkg_eventchannel_event_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DebugEvent); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_pkg_eventchannel_event_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_pkg_eventchannel_event_proto_goTypes, + DependencyIndexes: file_pkg_eventchannel_event_proto_depIdxs, + MessageInfos: file_pkg_eventchannel_event_proto_msgTypes, + }.Build() + File_pkg_eventchannel_event_proto = out.File + file_pkg_eventchannel_event_proto_rawDesc = nil + file_pkg_eventchannel_event_proto_goTypes = nil + file_pkg_eventchannel_event_proto_depIdxs = nil +} diff --git a/pkg/eventchannel/eventchannel_state_autogen.go b/pkg/eventchannel/eventchannel_state_autogen.go new file mode 100644 index 000000000..50b9c54b3 --- /dev/null +++ b/pkg/eventchannel/eventchannel_state_autogen.go @@ -0,0 +1,3 @@ +// automatically generated by stateify. + +package eventchannel |