func TestPluginEvents(t *testing.T) { iris.ResetDefault() var plugins = iris.Default.Plugins var prelistenran, postlistenran, precloseran bool plugins.Add(iris.PreListenFunc(func(*iris.Framework) { prelistenran = true })) plugins.Add(iris.PostListenFunc(func(*iris.Framework) { postlistenran = true })) plugins.Add(iris.PreCloseFunc(func(*iris.Framework) { precloseran = true })) myplugin := &testPluginEx{} plugins.Add(myplugin) if plugins.Len() != 4 { t.Fatalf("Expected: %d plugins to be registed but we got: %d", 4, plugins.Len()) } desc := plugins.GetDescription(myplugin) if desc != testPluginExDescription { t.Fatalf("Expected: %s as Description of the plugin but got: %s", testPluginExDescription, desc) } plugins.DoPreListen(nil) plugins.DoPostListen(nil) plugins.DoPreClose(nil) if !prelistenran { t.Fatalf("Expected to run PreListen Func but it doesn't!") } if !postlistenran { t.Fatalf("Expected to run PostListen Func but it doesn't!") } if !precloseran { t.Fatalf("Expected to run PostListen Func but it doesn't!") } if !myplugin.named { t.Fatalf("Plugin should be named with: %s!", testPluginExName) } if !myplugin.activated { t.Fatalf("Plugin should be activated but it's not!") } if !myplugin.prelistenran { t.Fatalf("Expected to run PreListen Struct but it doesn't!") } if !myplugin.postlistenran { t.Fatalf("Expected to run PostListen Struct but it doesn't!") } if !myplugin.precloseran { t.Fatalf("Expected to run PostListen Struct but it doesn't!") } }
func ExamplePlugins_Add() { iris.ResetDefault() iris.Default.Set(iris.OptionDisableBanner(true)) iris.Plugins.Add(iris.PreListenFunc(func(*iris.Framework) { fmt.Println("PreListen Func") })) iris.Plugins.Add(iris.PostListenFunc(func(*iris.Framework) { fmt.Println("PostListen Func") })) iris.Plugins.Add(iris.PreCloseFunc(func(*iris.Framework) { fmt.Println("PreClose Func") })) myplugin := &testPluginEx{} iris.Plugins.Add(myplugin) desc := iris.Plugins.GetDescription(myplugin) fmt.Println(desc) // travis have problems if I do that using // Listen(":8080") and Close() iris.Plugins.DoPreListen(iris.Default) iris.Plugins.DoPostListen(iris.Default) iris.Plugins.DoPreClose(iris.Default) // Output: // GetName Struct // Activate Struct // GetDescription Struct // Description for My test plugin // PreListen Func // PreListen Struct // PostListen Func // PostListen Struct // PreClose Func // PreClose Struct }