github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/wall_test.go (about)

     1  package db_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/pf-qiu/concourse/v6/atc"
     7  	"github.com/pf-qiu/concourse/v6/atc/db/dbfakes"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Wall", func() {
    14  	var (
    15  		msgOnly    = atc.Wall{Message: "this is a test message!"}
    16  		msgWithTTL = atc.Wall{Message: "this is a test message!", TTL: time.Minute}
    17  		startTime  = time.Now()
    18  	)
    19  
    20  	Context(" a message is set", func() {
    21  		BeforeEach(func() {
    22  			fakeClock = dbfakes.FakeClock{}
    23  			fakeClock.NowReturns(startTime)
    24  		})
    25  		Context("with no expiration", func() {
    26  			It("successfully sets the wall", func() {
    27  				err := dbWall.SetWall(msgOnly)
    28  				Expect(err).ToNot(HaveOccurred())
    29  				Expect(fakeClock.NowCallCount()).To(Equal(0))
    30  			})
    31  
    32  			It("successfully gets the wall", func() {
    33  				_ = dbWall.SetWall(msgOnly)
    34  				actualWall, err := dbWall.GetWall()
    35  				Expect(err).ToNot(HaveOccurred())
    36  				Expect(fakeClock.NowCallCount()).To(Equal(1))
    37  				Expect(actualWall).To(Equal(msgOnly))
    38  			})
    39  
    40  		})
    41  		Context("with an expiration", func() {
    42  			It("successfully sets the wall", func() {
    43  				err := dbWall.SetWall(msgWithTTL)
    44  				Expect(err).ToNot(HaveOccurred())
    45  				Expect(fakeClock.NowCallCount()).To(Equal(1))
    46  			})
    47  
    48  			Context("the message has not expired", func() {
    49  				Context("and gets a wall", func() {
    50  					BeforeEach(func() {
    51  						fakeClock.NowReturns(startTime.Add(time.Second))
    52  						fakeClock.UntilReturns(30 * time.Second)
    53  					})
    54  
    55  					Specify("successfully", func() {
    56  						_ = dbWall.SetWall(msgWithTTL)
    57  						_, err := dbWall.GetWall()
    58  						Expect(err).ToNot(HaveOccurred())
    59  						Expect(fakeClock.NowCallCount()).To(Equal(2))
    60  						Expect(fakeClock.UntilCallCount()).To(Equal(1))
    61  					})
    62  
    63  					Specify("with the TTL field set", func() {
    64  						_ = dbWall.SetWall(msgWithTTL)
    65  
    66  						actualWall, _ := dbWall.GetWall()
    67  						msgWithTTL.TTL = 30 * time.Second
    68  						Expect(actualWall).To(Equal(msgWithTTL))
    69  					})
    70  				})
    71  			})
    72  
    73  			Context("the message has expired", func() {
    74  				It("returns no message", func() {
    75  					_ = dbWall.SetWall(msgWithTTL)
    76  					fakeClock.NowReturns(startTime.Add(time.Hour))
    77  
    78  					actualWall, err := dbWall.GetWall()
    79  					Expect(err).ToNot(HaveOccurred())
    80  					Expect(fakeClock.NowCallCount()).To(Equal(2))
    81  					Expect(actualWall).To(Equal(atc.Wall{}))
    82  				})
    83  			})
    84  		})
    85  	})
    86  
    87  	Context("multiple messages are set", func() {
    88  		It("returns the last message that was set", func() {
    89  			expectedWall := atc.Wall{Message: "test 3"}
    90  			dbWall.SetWall(atc.Wall{Message: "test 1"})
    91  			dbWall.SetWall(atc.Wall{Message: "test 2"})
    92  			dbWall.SetWall(expectedWall)
    93  
    94  			actualWall, err := dbWall.GetWall()
    95  			Expect(err).ToNot(HaveOccurred())
    96  			Expect(actualWall).To(Equal(expectedWall))
    97  		})
    98  	})
    99  
   100  	Context("clearing the wall", func() {
   101  		BeforeEach(func() {
   102  			dbWall.SetWall(msgOnly)
   103  			actualWall, err := dbWall.GetWall()
   104  			Expect(err).ToNot(HaveOccurred())
   105  			Expect(actualWall).To(Equal(msgOnly), "ensure the message has been set before proceeding")
   106  		})
   107  		It("returns no error", func() {
   108  			err := dbWall.Clear()
   109  			Expect(err).ToNot(HaveOccurred())
   110  		})
   111  		It("GetWall returns no message after clearing the wall", func() {
   112  			_ = dbWall.Clear()
   113  
   114  			actualWall, err := dbWall.GetWall()
   115  			Expect(err).ToNot(HaveOccurred())
   116  			Expect(actualWall).To(Equal(atc.Wall{}))
   117  		})
   118  	})
   119  })