How to use Connection method of runner Package

Best Gauge code snippet using runner.Connection

connection_test.go

Source:connection_test.go Github

copy

Full Screen

...12 "github.com/hashicorp/terraform-provider-aws/internal/acctest"13 "github.com/hashicorp/terraform-provider-aws/internal/conns"14 tfapprunner "github.com/hashicorp/terraform-provider-aws/internal/service/apprunner"15)16func TestAccAppRunnerConnection_basic(t *testing.T) {17 rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)18 resourceName := "aws_apprunner_connection.test"19 resource.ParallelTest(t, resource.TestCase{20 PreCheck: func() { acctest.PreCheck(t); testAccPreCheckAppRunner(t) },21 ErrorCheck: acctest.ErrorCheck(t, apprunner.EndpointsID),22 Providers: acctest.Providers,23 CheckDestroy: testAccCheckConnectionDestroy,24 Steps: []resource.TestStep{25 {26 Config: testAccAppRunnerConnection_basic(rName),27 Check: resource.ComposeTestCheckFunc(28 testAccCheckConnectionExists(resourceName),29 acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "apprunner", regexp.MustCompile(fmt.Sprintf(`connection/%s/.+`, rName))),30 resource.TestCheckResourceAttr(resourceName, "connection_name", rName),31 resource.TestCheckResourceAttr(resourceName, "provider_type", apprunner.ProviderTypeGithub),32 resource.TestCheckResourceAttr(resourceName, "status", apprunner.ConnectionStatusPendingHandshake),33 resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),34 ),35 },36 {37 ResourceName: resourceName,38 ImportState: true,39 ImportStateVerify: true,40 },41 },42 })43}44func TestAccAppRunnerConnection_disappears(t *testing.T) {45 rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)46 resourceName := "aws_apprunner_connection.test"47 resource.ParallelTest(t, resource.TestCase{48 PreCheck: func() { acctest.PreCheck(t); testAccPreCheckAppRunner(t) },49 ErrorCheck: acctest.ErrorCheck(t, apprunner.EndpointsID),50 Providers: acctest.Providers,51 CheckDestroy: testAccCheckConnectionDestroy,52 Steps: []resource.TestStep{53 {54 Config: testAccAppRunnerConnection_basic(rName),55 Check: resource.ComposeTestCheckFunc(56 testAccCheckConnectionExists(resourceName),57 acctest.CheckResourceDisappears(acctest.Provider, tfapprunner.ResourceConnection(), resourceName),58 ),59 ExpectNonEmptyPlan: true,60 },61 },62 })63}64func TestAccAppRunnerConnection_tags(t *testing.T) {65 rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)66 resourceName := "aws_apprunner_connection.test"67 resource.ParallelTest(t, resource.TestCase{68 PreCheck: func() { acctest.PreCheck(t); testAccPreCheckAppRunner(t) },69 ErrorCheck: acctest.ErrorCheck(t, apprunner.EndpointsID),70 Providers: acctest.Providers,71 CheckDestroy: testAccCheckConnectionDestroy,72 Steps: []resource.TestStep{73 {74 Config: testAccAppRunnerConnectionConfigTags1(rName, "key1", "value1"),75 Check: resource.ComposeTestCheckFunc(76 testAccCheckConnectionExists(resourceName),77 resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),78 resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),79 resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"),80 resource.TestCheckResourceAttr(resourceName, "tags_all.key1", "value1"),81 ),82 },83 {84 ResourceName: resourceName,85 ImportState: true,86 ImportStateVerify: true,87 },88 {89 Config: testAccAppRunnerConnectionConfigTags2(rName, "key1", "value1updated", "key2", "value2"),90 Check: resource.ComposeTestCheckFunc(91 testAccCheckConnectionExists(resourceName),92 resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),93 resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),94 resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),95 resource.TestCheckResourceAttr(resourceName, "tags_all.%", "2"),96 resource.TestCheckResourceAttr(resourceName, "tags_all.key1", "value1updated"),97 resource.TestCheckResourceAttr(resourceName, "tags_all.key2", "value2"),98 ),99 },100 {101 Config: testAccAppRunnerConnectionConfigTags1(rName, "key2", "value2"),102 Check: resource.ComposeTestCheckFunc(103 testAccCheckConnectionExists(resourceName),104 resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),105 resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),106 resource.TestCheckResourceAttr(resourceName, "tags_all.%", "1"),107 resource.TestCheckResourceAttr(resourceName, "tags_all.key2", "value2"),108 ),109 },110 },111 })112}113func testAccCheckConnectionDestroy(s *terraform.State) error {114 for _, rs := range s.RootModule().Resources {115 if rs.Type != "aws_apprunner_connection" {116 continue117 }118 conn := acctest.Provider.Meta().(*conns.AWSClient).AppRunnerConn119 connection, err := tfapprunner.FindConnectionSummaryByName(context.Background(), conn, rs.Primary.ID)120 if tfawserr.ErrCodeEquals(err, apprunner.ErrCodeResourceNotFoundException) {121 continue122 }123 if err != nil {124 return err125 }126 if connection != nil {127 return fmt.Errorf("App Runner Connection (%s) still exists", rs.Primary.ID)128 }129 }130 return nil131}132func testAccCheckConnectionExists(n string) resource.TestCheckFunc {133 return func(s *terraform.State) error {134 rs, ok := s.RootModule().Resources[n]135 if !ok {136 return fmt.Errorf("Not found: %s", n)137 }138 if rs.Primary.ID == "" {139 return fmt.Errorf("No App Runner Connection ID is set")140 }141 conn := acctest.Provider.Meta().(*conns.AWSClient).AppRunnerConn142 connection, err := tfapprunner.FindConnectionSummaryByName(context.Background(), conn, rs.Primary.ID)143 if err != nil {144 return err145 }146 if connection == nil {147 return fmt.Errorf("App Runner Connection (%s) not found", rs.Primary.ID)148 }149 return nil150 }151}152func testAccAppRunnerConnection_basic(rName string) string {153 return fmt.Sprintf(`154resource "aws_apprunner_connection" "test" {155 connection_name = %q156 provider_type = "GITHUB"157}158`, rName)159}160func testAccAppRunnerConnectionConfigTags1(rName string, tagKey1 string, tagValue1 string) string {161 return fmt.Sprintf(`162resource "aws_apprunner_connection" "test" {163 connection_name = %[1]q164 provider_type = "GITHUB"165 tags = {166 %[2]q = %[3]q167 }168}169`, rName, tagKey1, tagValue1)170}171func testAccAppRunnerConnectionConfigTags2(rName string, tagKey1 string, tagValue1 string, tagKey2 string, tagValue2 string) string {172 return fmt.Sprintf(`173resource "aws_apprunner_connection" "test" {174 connection_name = %[1]q175 provider_type = "GITHUB"176 tags = {177 %[2]q = %[3]q178 %[4]q = %[5]q179 }180}181`, rName, tagKey1, tagValue1, tagKey2, tagValue2)182}...

Full Screen

Full Screen

carrot.go

Source:carrot.go Github

copy

Full Screen

...7 "github.com/ar3s3ru/go-carrot/listener"8 "github.com/ar3s3ru/go-carrot/topology"9 "github.com/streadway/amqp"10)11// ErrNoConnection is returned by Runner.Run when no valid AMQP connection12// has been specified.13var ErrNoConnection = errors.New("carrot: no connection provided")14// ErrNoHandler is returned by Runner.Run when no handler has been specified,15// so that Runner.Run can't handle any incoming messages.16var ErrNoHandler = errors.New("carrot: no handler specified")17// ErrNoListener is returned by Runner.Run when no delivery listener18// has been specified, so that Runner.Run can't receive any messages19// from the AMQP broker.20var ErrNoListener = errors.New("carrot: no listener specified")21// Closer allows to close the amqp.Connection provided and22// any active Listener after Runner.Run has called.23type Closer struct {24 conn listener.Connection25 closer listener.Closer26}27// Close closes both the amqp.Connection provided and the Listener28// declared in the Runner.29func (closer Closer) Close(ctx context.Context) error {30 defer closer.conn.Close()31 return closer.closer.Close(ctx)32}33// Closed returns a channel that gets closed when the Listener gets closed.34//35// Useful to wait for consumers completion.36func (closer Closer) Closed() <-chan error {37 return closer.closer.Closed()38}39// Runner instruments all the different parts of the go-carrot library,40// provided with a valid AMQP connection.41type Runner struct {42 conn listener.Connection43 declarer topology.Declarer44 handler handler.Handler45 listener listener.Listener46 shutdown *Shutdown47 gracefulShutdown bool48}49// Run starts all the different parts of the Runner instrumentator,50// in the following order: topology declaration, delivery listener and messages listener.51//52// Message listener uses the sink channel coming from the delivery listener,53// and spawns a separate worker goroutine to run the message handler54// specified during configuration with the new amqp.Delivery received.55//56// An error is returned if the supplied parameters during configuration are not57// valid, or if something happened on the AMQP connection.58func (runner Runner) Run() (Closer, error) {59 if runner.conn == nil {60 return Closer{}, ErrNoConnection61 }62 if runner.declarer != nil {63 if err := runner.declareTopology(); err != nil {64 return Closer{}, fmt.Errorf("carrot: failed to declare topology, %w", err)65 }66 }67 // No handler nor delivery listener is an acceptable scenario: it means68 // the user is not leveraging carrot for message consumption.69 if runner.handler == nil && runner.listener == nil {70 return Closer{}, nil71 }72 closer, err := runner.listenAndServe()73 if err != nil {74 return Closer{}, fmt.Errorf("carrot: failed to listen and serve consumers, %w", err)75 }76 runnerCloser := Closer{77 conn: runner.conn,78 closer: closer,79 }80 if runner.gracefulShutdown {81 go gracefulShutdown(runnerCloser, runner.shutdown.orDefault())82 }83 return runnerCloser, nil84}85func (runner Runner) declareTopology() error {86 ch, err := runner.openChannel()87 if err != nil {88 return err89 }90 defer ch.Close()91 return runner.declarer.Declare(ch)92}93func (runner Runner) listenAndServe() (listener.Closer, error) {94 if runner.handler == nil {95 return nil, ErrNoHandler96 }97 if runner.listener == nil {98 return nil, ErrNoListener99 }100 ch, err := runner.openChannel()101 if err != nil {102 return nil, err103 }104 closer, err := runner.listener.Listen(runner.conn, ch, runner.handler)105 if err != nil {106 return nil, fmt.Errorf("carrot: failed to listen, %w", err)107 }108 return closer, nil109}110func (runner Runner) openChannel() (*amqp.Channel, error) {111 ch, err := runner.conn.Channel()112 if err != nil {113 return nil, fmt.Errorf("carrot: failed to create channel from connection, %w", err)114 }115 return ch, nil116}117// Run is a convenience method to run a new Runner instance directly.118//119// It is the equivalent of carrot.From(...).Run()120func Run(conn listener.Connection, options ...Option) (Closer, error) {121 return From(conn, options...).Run()122}123// From creates a new Runner instance, given an AMQP connection and options.124//125// Required options are WithListener, to bind a channel to an amqp.Delivery sink126// and start receiving messages, and WithHandler, to handle all the incoming127// messages.128func From(conn listener.Connection, options ...Option) Runner {129 runner := Runner{conn: conn}130 for _, option := range options {131 if option == nil {132 continue133 }134 option(&runner)135 }136 return runner137}138// Option represents an additional argument for the Runner factory method.139type Option func(*Runner)140// WithTopology adds a topology declaration step to the new Runner instance.141func WithTopology(declarer topology.Declarer) Option {142 return func(runner *Runner) { runner.declarer = declarer }...

Full Screen

Full Screen

conn.go

Source:conn.go Github

copy

Full Screen

...11func initDB() (*runner.DB, error) {12 config := configuration.LoadConfiguration("config.json")13 runner, err := connectWIthTimeout(config.PsqlConfig)14 if err != nil {15 return nil, newBadConnectionError(err)16 }17 return runner, nil18}19func connectWIthTimeout(config configuration.PsqlConfiguration) (*runner.DB, error) {20 connectionString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", config.Username, config.Password, config.Hostname, config.Port, config.DatabaseName, config.SslMode)21 log.Print(connectionString)22 log.Printf("Matt connstring: %s", connectionString)23 // Before returning a connection we will try to ping the database to verify connection24 var dbRunner *runner.DB25 connected := make(chan error)26 go func() {27 db, err := sql.Open(driverName, connectionString)28 if err != nil {29 connected <- err30 }31 runner.MustPing(db)32 dbRunner = runner.NewDB(db, driverName)33 connected <- nil34 }()35 select {36 case result := <-connected:37 return dbRunner, result38 case <-time.After(time.Duration(10) * time.Second):39 return nil, newConnectionTimeoutError()40 }41}...

Full Screen

Full Screen

Connection

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 r := runner.New(10 * time.Second)5 r.Add(createTask(), createTask(), createTask())6 if err := r.Start(); err != nil {7 switch err {8 fmt.Println("Terminating due to timeout")9 os.Exit(1)10 fmt.Println("Terminating due to interrupt")11 os.Exit(2)12 }13 }14 fmt.Println("Process ended")15}16func createTask() func(int) {17 return func(id int) {18 fmt.Printf("Processor - Task #%d.\n", id)19 time.Sleep(time.Duration(id) * time.Second)20 }21}22import (23var ErrTimeout = errors.New("received timeout")24var ErrInterrupt = errors.New("received interrupt")25type Runner struct {26 tasks []func(int)27}28func New(d time.Duration) *Runner {29 return &Runner{30 interrupt: make(chan os.Signal, 1),31 complete: make(chan error),32 timeout: time.After(d),33 }34}35func (r *Runner) Add(tasks ...func(int)) {36 r.tasks = append(r.tasks, tasks...)37}38func (r *Runner) Start() error {39 signal.Notify(r.interrupt, os.Interrupt)40 go func() {41 r.complete <- r.run()42 }()43 select {44 }45}46func (r *Runner) run() error {47 for id, task := range r.tasks {48 if r.gotInterrupt() {49 }50 task(id)51 }52}53func (r *Runner) gotInterrupt() bool {54 select {55 signal.Stop(r.interrupt)56 }57}

Full Screen

Full Screen

Connection

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := runner.New(3 * time.Second)4 r.Add(createTask(), createTask(), createTask())5 if err := r.Start(); err != nil {6 switch err {7 fmt.Println("Terminating due to timeout")8 os.Exit(1)9 fmt.Println("Terminating due to interrupt")10 os.Exit(2)11 }12 }13 fmt.Println("Process completed")14}15func createTask() func(int) {16 return func(id int) {17 fmt.Printf("Processor - Task #%d.\n", id)18 time.Sleep(time.Duration(id) * time.Second)19 }20}

Full Screen

Full Screen

Connection

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Start")4 r := runner.New(3 * time.Second)5 r.Add(createTask(), createTask(), createTask())6 if err := r.Start(); err != nil {7 switch err {8 fmt.Println("Terminating due to timeout")9 os.Exit(1)10 fmt.Println("Terminating due to interrupt")11 os.Exit(2)12 }13 }14 fmt.Println("End")15}

Full Screen

Full Screen

Connection

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("1.go")4 r := runner.New("localhost:8080")5 r.Connect()6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful