├── AUTHORS ├── CONTRIBUTING ├── LICENSE ├── README ├── component.go ├── component_benchmark_test.go ├── component_test.go ├── event.go ├── event_handler.go ├── main.go └── utils.go /AUTHORS: -------------------------------------------------------------------------------- 1 | # This is the list of Go-Circuits authors for copyright purposes. 2 | # 3 | # This does not necessarily list everyone who has contributed code, since in 4 | # some cases, their employer may be the copyright holder. To see the full list 5 | # of contributors, see the revision history in source control. 6 | Google Inc. 7 | 8 | -------------------------------------------------------------------------------- /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at the end). 2 | 3 | ### Before you contribute 4 | Before we can use your code, you must sign the 5 | [Google Individual Contributor License Agreement] 6 | (https://cla.developers.google.com/about/google-individual) 7 | (CLA), which you can do online. The CLA is necessary mainly because you own the 8 | copyright to your changes, even after your contribution becomes part of our 9 | codebase, so we need your permission to use and distribute your code. We also 10 | need to be sure of various other things—for instance that you'll tell us if you 11 | know that your code infringes on other people's patents. You don't have to sign 12 | the CLA until after you've submitted your code for review and a member has 13 | approved it, but you must do it before we can put your code into our codebase. 14 | Before you start working on a larger contribution, you should get in touch with 15 | us first through the issue tracker with your idea so that we can help out and 16 | possibly guide you. Coordinating up front makes it much easier to avoid 17 | frustration later on. 18 | 19 | ### Code reviews 20 | All submissions, including submissions by project members, require review. We 21 | use Github pull requests for this purpose. 22 | 23 | ### The small print 24 | Contributions made by corporations are covered by a different agreement than 25 | the one above, the 26 | [Software Grant and Corporate Contributor License Agreement] 27 | (https://cla.developers.google.com/about/google-corporate). 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | An Event driven Component framework for Golang based on Circuits 2 | (http://github.com/circuits/circuits). 3 | 4 | DISCLAIMER: This is not an official Google product. 5 | 6 | Basic Usage 7 | 8 | import ( 9 | "fmt" 10 | "go-circuits" 11 | ) 12 | 13 | type MyComponent struct { 14 | go-circuits.Component 15 | } 16 | 17 | func (mc *MyComponent) HelloWorldEventHandler(_ go-circuits.Event) { 18 | fmt.Println("Hello World!") 19 | } 20 | 21 | func NewMyComponent() *MyComponent { 22 | mc := MyComponent{} 23 | mc.RegisterEventHandler(go-circuits.NewEventHandler( 24 | "hello_world", 25 | mc.HelloWorldEventHandler 26 | )) 27 | return &mc 28 | } 29 | 30 | func main() { 31 | mc := NewMyComponent() 32 | mc.Fire(go-circuits.BaseEvent{"hello_world"}) 33 | mc.Run() 34 | } 35 | 36 | -------------------------------------------------------------------------------- /component.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "container/list" 19 | "sync" 20 | ) 21 | 22 | type ExitEvent struct { 23 | BaseEvent 24 | Exit func() 25 | } 26 | 27 | func NewComponent() *Component { 28 | return NewAdvancedComponent(100) 29 | } 30 | 31 | func NewAdvancedComponent(eventChanSize int) *Component { 32 | c := Component{ 33 | eventHandlers: make(map[string]map[string]*list.List), 34 | eventChan: make(chan Event, eventChanSize), 35 | } 36 | c.RegisterEventHandler(NewEventHandler("*", "exit", c.Exit)) 37 | return &c 38 | } 39 | 40 | type Component struct { 41 | root *Component 42 | parent *Component 43 | children []*Component 44 | eventHandlers map[string]map[string]*list.List 45 | eventChan chan Event 46 | waitGroup sync.WaitGroup 47 | } 48 | 49 | // Add an EventHandler to the system 50 | func (c *Component) RegisterEventHandler(handler *EventHandler) { 51 | channel := handler.Channel() 52 | target := handler.Target() 53 | channel_handlers, ok := c.eventHandlers[channel] 54 | if !ok { 55 | channel_handlers = make(map[string]*list.List) 56 | c.eventHandlers[channel] = channel_handlers 57 | } 58 | handlers, ok := channel_handlers[target] 59 | if !ok { 60 | handlers = list.New() 61 | channel_handlers[target] = handlers 62 | } 63 | handlers.PushBack(handler) 64 | 65 | if c.parent != nil { 66 | c.parent.RegisterEventHandler(handler) 67 | } 68 | } 69 | 70 | // Remove an EventHandler from the system 71 | func (c *Component) UnregisterEventHandler(handler *EventHandler) { 72 | channel := handler.Channel() 73 | target := handler.Target() 74 | handlers, ok := c.eventHandlers[channel][target] 75 | if !ok { 76 | return 77 | } 78 | 79 | for h := handlers.Front(); h != nil; h = h.Next() { 80 | if h.Value.(*EventHandler) == handler { 81 | handlers.Remove(h) 82 | break 83 | } 84 | } 85 | 86 | if c.parent != nil { 87 | c.parent.UnregisterEventHandler(handler) 88 | } 89 | } 90 | 91 | // Add a child Component to the system, implicitly adds all of children recursively 92 | func (c *Component) RegisterComponent(component *Component) { 93 | c.children = append(c.children, component) 94 | component.root = c.root 95 | component.parent = c 96 | 97 | for _, channel_handlers := range component.eventHandlers { 98 | for _, handlers := range channel_handlers { 99 | for h := handlers.Front(); h != nil; h = h.Next() { 100 | c.RegisterEventHandler(h.Value.(*EventHandler)) 101 | } 102 | } 103 | } 104 | } 105 | 106 | // Remove a Component from the system, implicitly removes all children recursively 107 | func (c *Component) UnregisterComponent(component *Component) { 108 | found := false 109 | i := 0 110 | for ; i < len(c.children); i++ { 111 | if c.children[i] == component { 112 | found = true 113 | break 114 | } 115 | } 116 | 117 | if found { 118 | c.children = append(c.children[:i], c.children[i+1:]...) 119 | for _, channel_handlers := range component.eventHandlers { 120 | for _, handlers := range channel_handlers { 121 | for h := handlers.Front(); h != nil; h = h.Next() { 122 | c.UnregisterEventHandler(h.Value.(*EventHandler)) 123 | } 124 | } 125 | } 126 | } 127 | } 128 | 129 | // Add an event to the queue 130 | func (c *Component) Fire(event Event) { 131 | c.eventChan <- event 132 | } 133 | 134 | // Trigger an exit on the next loop 135 | func (c *Component) Exit(_ Event) { 136 | close(c.eventChan) 137 | } 138 | 139 | // Process events piped over the channel 140 | func (c *Component) processEvents() { 141 | defer c.waitGroup.Done() 142 | for true { 143 | event, open := <-c.eventChan 144 | if !open { 145 | break 146 | } 147 | c.processEvent(event) 148 | } 149 | } 150 | 151 | // Process a single event 152 | func (c *Component) processEvent(event Event) { 153 | channel := event.Channel() 154 | target := event.Target() 155 | handlers := c.getEventHandlers(channel, target) 156 | for _, h := range handlers { 157 | err := h.Call(event) 158 | 159 | // Send notifications of Event status 160 | if event.NotifyFailure() && err != nil { 161 | c.Fire(NewEvent(channel, target+"_failure")) 162 | } else if event.NotifySuccess() && err == nil { 163 | c.Fire(NewEvent(channel, target+"_success")) 164 | } 165 | if event.NotifyComplete() { 166 | c.Fire(NewEvent(channel, target+"_complete")) 167 | } 168 | } 169 | } 170 | 171 | // Determine event handlers for an event 172 | func (c *Component) getEventHandlers(channel, target string) []*EventHandler { 173 | handlers := list.New() 174 | if channel == "*" { 175 | for _, channel_handlers := range c.eventHandlers { 176 | c.addEventHandlersFromChannel(handlers, channel_handlers, target) 177 | } 178 | } else { 179 | c.addEventHandlersFromChannel(handlers, c.eventHandlers[channel], target) 180 | c.addEventHandlersFromChannel(handlers, c.eventHandlers["*"], target) 181 | } 182 | 183 | ret := make([]*EventHandler, 0, handlers.Len()) 184 | for h := handlers.Front(); h != nil; h = h.Next() { 185 | ret = append(ret, h.Value.(*EventHandler)) 186 | } 187 | return ret 188 | } 189 | 190 | func (c *Component) addEventHandlersFromChannel(handlers *list.List, channel_handlers map[string]*list.List, target string) { 191 | if target == "*" { 192 | for _, target_handlers := range channel_handlers { 193 | if target_handlers != nil { 194 | handlers.PushBackList(target_handlers) 195 | } 196 | } 197 | } else { 198 | target_handlers := channel_handlers[target] 199 | if target_handlers != nil { 200 | handlers.PushBackList(target_handlers) 201 | } 202 | generic_handlers := channel_handlers["*"] 203 | if generic_handlers != nil { 204 | handlers.PushBackList(generic_handlers) 205 | } 206 | } 207 | } 208 | 209 | // Main loop 210 | func (c *Component) Run(num_workers int) { 211 | if c.root != nil { 212 | panic("Cannot run main loop on child component.") 213 | } 214 | 215 | for i := 0; i < num_workers; i++ { 216 | c.waitGroup.Add(1) 217 | go c.processEvents() 218 | } 219 | 220 | c.waitGroup.Wait() // Wait for all event goroutines to finish 221 | } 222 | -------------------------------------------------------------------------------- /component_benchmark_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "runtime" 19 | "sync" 20 | "testing" 21 | ) 22 | 23 | func benchmark(b *testing.B, threads int) { 24 | c := NewComponent() 25 | c.RegisterEventHandler(NewEventHandler("*", "f", func(_ Event) {})) 26 | wg := &sync.WaitGroup{} 27 | wg.Add(1) 28 | go async_run(threads, c, wg) 29 | b.ResetTimer() 30 | for i := 0; i < b.N; i++ { 31 | c.Fire(NewEvent("*", "f")) 32 | } 33 | c.Fire(NewEvent("*", "exit")) 34 | wg.Wait() 35 | } 36 | 37 | func Benchmark_SingleThread(b *testing.B) { 38 | benchmark(b, 1) 39 | } 40 | 41 | func Benchmark_MultiThread(b *testing.B) { 42 | benchmark(b, runtime.GOMAXPROCS(0)) 43 | } 44 | -------------------------------------------------------------------------------- /component_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "testing" 19 | ) 20 | 21 | var calls []string 22 | 23 | func BasicEventHandler(e Event) { 24 | calls = append(calls, e.Target()) 25 | } 26 | 27 | func FailingEventHandler(e Event) { 28 | calls = append(calls, e.Target()) 29 | panic("Gophers in the engine room!") 30 | } 31 | 32 | func Test_SimpleEvent(t *testing.T) { 33 | calls = make([]string, 0) 34 | c := NewComponent() 35 | c.RegisterEventHandler(NewEventHandler("*", "foo", BasicEventHandler)) 36 | c.Fire(NewEvent("*", "foo")) 37 | c.Fire(NewEvent("*", "exit")) 38 | c.Run(1) 39 | if len(calls) != 1 { 40 | t.Errorf("Expected one call to the EventHandler. Got %d.", len(calls)) 41 | } 42 | } 43 | 44 | func Test_EventsFIFO(t *testing.T) { 45 | calls = make([]string, 0) 46 | c := NewComponent() 47 | c.RegisterEventHandler(NewEventHandler("*", "foo", BasicEventHandler)) 48 | c.RegisterEventHandler(NewEventHandler("*", "bar", BasicEventHandler)) 49 | c.Fire(NewEvent("*", "foo")) 50 | c.Fire(NewEvent("*", "bar")) 51 | c.Fire(NewEvent("*", "exit")) 52 | c.Run(1) 53 | if len(calls) != 2 { 54 | t.Errorf("Expected two calls to the EventHandler. Got %d.", len(calls)) 55 | } else if calls[0] != "foo" || calls[1] != "bar" { 56 | t.Error("Expected event \"foo\" to occur before event \"bar\".") 57 | } 58 | } 59 | 60 | func Test_EventOnSpecificChannel(t *testing.T) { 61 | calls = make([]string, 0) 62 | c := NewComponent() 63 | c.RegisterEventHandler(NewEventHandler("foo", "event", BasicEventHandler)) 64 | c.RegisterEventHandler(NewEventHandler("bar", "event", BasicEventHandler)) 65 | c.Fire(NewEvent("foo", "event")) 66 | c.Fire(NewEvent("*", "exit")) 67 | c.Run(1) 68 | if len(calls) != 1 { 69 | t.Errorf("Expected one call to the EventHandler. Got %d.", len(calls)) 70 | } 71 | } 72 | 73 | func Test_EventOnGlobalChannel(t *testing.T) { 74 | calls = make([]string, 0) 75 | c := NewComponent() 76 | c.RegisterEventHandler(NewEventHandler("foo", "event", BasicEventHandler)) 77 | c.RegisterEventHandler(NewEventHandler("bar", "event", BasicEventHandler)) 78 | c.Fire(NewEvent("*", "event")) 79 | c.Fire(NewEvent("*", "exit")) 80 | c.Run(1) 81 | if len(calls) != 2 { 82 | t.Errorf("Expected one call to the EventHandler. Got %d.", len(calls)) 83 | } 84 | } 85 | 86 | func Test_HandlerOnGlobalChannel(t *testing.T) { 87 | calls = make([]string, 0) 88 | c := NewComponent() 89 | c.RegisterEventHandler(NewEventHandler("*", "event", BasicEventHandler)) 90 | c.Fire(NewEvent("foo", "event")) 91 | c.Fire(NewEvent("*", "exit")) 92 | c.Run(1) 93 | if len(calls) != 1 { 94 | t.Errorf("Expected one call to the EventHandler. Got %d.", len(calls)) 95 | } 96 | } 97 | 98 | func Test_EventOnSpecificTarget(t *testing.T) { 99 | calls = make([]string, 0) 100 | c := NewComponent() 101 | c.RegisterEventHandler(NewEventHandler("*", "foo", BasicEventHandler)) 102 | c.RegisterEventHandler(NewEventHandler("*", "bar", BasicEventHandler)) 103 | c.Fire(NewEvent("*", "foo")) 104 | c.Fire(NewEvent("*", "exit")) 105 | c.Run(1) 106 | if len(calls) != 1 { 107 | t.Errorf("Expected one call to the EventHandler. Got %d.", len(calls)) 108 | } 109 | } 110 | 111 | func Test_EventOnGenericTarget(t *testing.T) { 112 | calls = make([]string, 0) 113 | c := NewComponent() 114 | c.RegisterEventHandler(NewEventHandler("*", "foo", BasicEventHandler)) 115 | c.Fire(NewEvent("*", "*")) 116 | c.Run(1) 117 | if len(calls) != 1 { 118 | t.Errorf("Expected one call to the EventHandler. Got %d.", len(calls)) 119 | } 120 | } 121 | 122 | func Test_HandlerOnGenericTarget(t *testing.T) { 123 | calls = make([]string, 0) 124 | c := NewComponent() 125 | c.RegisterEventHandler(NewEventHandler("*", "*", BasicEventHandler)) 126 | c.Fire(NewEvent("*", "foo")) 127 | c.Fire(NewEvent("*", "exit")) 128 | c.Run(1) 129 | if len(calls) != 2 { 130 | t.Errorf("Expected two calls to the EventHandler. Got %d.", len(calls)) 131 | } 132 | } 133 | 134 | func Test_UnregisterEventHandler(t *testing.T) { 135 | calls = make([]string, 0) 136 | c := NewComponent() 137 | eventHandler := NewEventHandler("*", "foo", BasicEventHandler) 138 | c.RegisterEventHandler(eventHandler) 139 | c.UnregisterEventHandler(eventHandler) 140 | c.Fire(NewEvent("*", "foo")) 141 | c.Fire(NewEvent("*", "exit")) 142 | c.Run(1) 143 | if len(calls) != 0 { 144 | t.Errorf("Expected no calls to the EventHandler. Got %d.", len(calls)) 145 | } 146 | } 147 | 148 | func Test_RegisterComponent(t *testing.T) { 149 | calls = make([]string, 0) 150 | main := NewComponent() 151 | child := NewComponent() 152 | child.RegisterEventHandler(NewEventHandler("*", "foo", BasicEventHandler)) 153 | main.RegisterComponent(child) 154 | main.Fire(NewEvent("*", "foo")) 155 | main.Fire(NewEvent("*", "exit")) 156 | main.Run(1) 157 | if len(calls) != 1 { 158 | t.Errorf("Expected one call to the event handler. Got %d.", len(calls)) 159 | } 160 | } 161 | 162 | func Test_UnregisterComponent(t *testing.T) { 163 | calls = make([]string, 0) 164 | main := NewComponent() 165 | child := NewComponent() 166 | child.RegisterEventHandler(NewEventHandler("*", "foo", BasicEventHandler)) 167 | main.RegisterComponent(child) 168 | main.UnregisterComponent(child) 169 | main.Fire(NewEvent("*", "foo")) 170 | main.Fire(NewEvent("*", "exit")) 171 | main.Run(1) 172 | if len(calls) != 0 { 173 | t.Errorf("Expected no calls to the event handler. Got %d.", len(calls)) 174 | } 175 | } 176 | 177 | func Test_CompleteNotification(t *testing.T) { 178 | calls = make([]string, 0) 179 | c := NewComponent() 180 | eventHandler := NewEventHandler("*", "foo", BasicEventHandler) 181 | c.RegisterEventHandler(eventHandler) 182 | completeHandler := NewEventHandler("*", "foo_complete", BasicEventHandler) 183 | c.RegisterEventHandler(completeHandler) 184 | e := NewEvent("*", "foo") 185 | e.SetNotifyComplete(true) 186 | c.Fire(e) 187 | c.Fire(NewEvent("*", "exit")) 188 | c.Run(1) 189 | if len(calls) != 2 { 190 | t.Errorf("Expected two calls to the event handler. Got %d.", len(calls)) 191 | } 192 | } 193 | 194 | func Test_SuccessNotification(t *testing.T) { 195 | calls = make([]string, 0) 196 | c := NewComponent() 197 | eventHandler := NewEventHandler("*", "foo", BasicEventHandler) 198 | c.RegisterEventHandler(eventHandler) 199 | completeHandler := NewEventHandler("*", "foo_success", BasicEventHandler) 200 | c.RegisterEventHandler(completeHandler) 201 | e := NewEvent("*", "foo") 202 | e.SetNotifySuccess(true) 203 | c.Fire(e) 204 | c.Fire(NewEvent("*", "exit")) 205 | c.Run(1) 206 | if len(calls) != 2 { 207 | t.Errorf("Expected two calls to the event handler. Got %d.", len(calls)) 208 | } 209 | } 210 | 211 | func Test_FailureNotification(t *testing.T) { 212 | calls = make([]string, 0) 213 | c := NewComponent() 214 | eventHandler := NewEventHandler("*", "foo", FailingEventHandler) 215 | c.RegisterEventHandler(eventHandler) 216 | completeHandler := NewEventHandler("*", "foo_failure", BasicEventHandler) 217 | c.RegisterEventHandler(completeHandler) 218 | e := NewEvent("*", "foo") 219 | e.SetNotifyFailure(true) 220 | c.Fire(e) 221 | c.Fire(NewEvent("*", "exit")) 222 | c.Run(1) 223 | if len(calls) != 2 { 224 | t.Errorf("Expected two calls to the event handler. Got %d.", len(calls)) 225 | } 226 | } 227 | -------------------------------------------------------------------------------- /event.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | func NewEvent(channel, target string) Event { 18 | return &BaseEvent{channel: channel, target: target} 19 | } 20 | 21 | type Event interface { 22 | Channel() string 23 | Target() string 24 | SetNotifyFailure(bool) 25 | NotifyFailure() bool 26 | SetNotifySuccess(bool) 27 | NotifySuccess() bool 28 | SetNotifyComplete(bool) 29 | NotifyComplete() bool 30 | } 31 | 32 | type BaseEvent struct { 33 | channel string 34 | target string 35 | notify_failure bool 36 | notify_success bool 37 | notify_complete bool 38 | } 39 | 40 | func (e *BaseEvent) Channel() string { 41 | return e.channel 42 | } 43 | 44 | func (e *BaseEvent) Target() string { 45 | return e.target 46 | } 47 | 48 | func (e *BaseEvent) SetNotifyFailure(v bool) { 49 | e.notify_failure = v 50 | } 51 | 52 | func (e *BaseEvent) NotifyFailure() bool { 53 | return e.notify_failure 54 | } 55 | 56 | func (e *BaseEvent) SetNotifySuccess(v bool) { 57 | e.notify_success = v 58 | } 59 | 60 | func (e *BaseEvent) NotifySuccess() bool { 61 | return e.notify_success 62 | } 63 | 64 | func (e *BaseEvent) SetNotifyComplete(v bool) { 65 | e.notify_complete = v 66 | } 67 | 68 | func (e *BaseEvent) NotifyComplete() bool { 69 | return e.notify_complete 70 | } 71 | -------------------------------------------------------------------------------- /event_handler.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "errors" 19 | ) 20 | 21 | func NewEventHandler(channel string, target string, c func(Event)) *EventHandler { 22 | eh := EventHandler{call: c, channel: channel, target: target} 23 | return &eh 24 | } 25 | 26 | type EventHandler struct { 27 | call func(Event) 28 | channel string 29 | target string 30 | } 31 | 32 | func (eh *EventHandler) Channel() string { 33 | return eh.channel 34 | } 35 | 36 | func (eh *EventHandler) Target() string { 37 | return eh.target 38 | } 39 | 40 | func (eh *EventHandler) Call(event Event) (err error) { 41 | defer func() { 42 | if err_s := recover(); err_s != nil { 43 | err = errors.New(err_s.(string)) 44 | } 45 | }() 46 | eh.call(event) 47 | return 48 | } 49 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | func main() { 18 | } 19 | -------------------------------------------------------------------------------- /utils.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 the Go-Circuits Authors. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "sync" 19 | ) 20 | 21 | func async_run(threads int, c *Component, wg *sync.WaitGroup) { 22 | defer wg.Done() 23 | c.Run(threads) 24 | } 25 | --------------------------------------------------------------------------------