Best Selenoid code snippet using protect.New
branches.go
Source:branches.go
...356 protectedBranch := &ProtectedBranch{357 RepoID: repo.ID,358 ID: id,359 }360 sess := x.NewSession()361 defer sess.Close()362 if err = sess.Begin(); err != nil {363 return err364 }365 if affected, err := sess.Delete(protectedBranch); err != nil {366 return err367 } else if affected != 1 {368 return fmt.Errorf("delete protected branch ID(%v) failed", id)369 }370 return sess.Commit()371}372// DeletedBranch struct373type DeletedBranch struct {374 ID int64 `xorm:"pk autoincr"`375 RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`376 Name string `xorm:"UNIQUE(s) NOT NULL"`377 Commit string `xorm:"UNIQUE(s) NOT NULL"`378 DeletedByID int64 `xorm:"INDEX"`379 DeletedBy *User `xorm:"-"`380 DeletedUnix timeutil.TimeStamp `xorm:"INDEX created"`381}382// AddDeletedBranch adds a deleted branch to the database383func (repo *Repository) AddDeletedBranch(branchName, commit string, deletedByID int64) error {384 deletedBranch := &DeletedBranch{385 RepoID: repo.ID,386 Name: branchName,387 Commit: commit,388 DeletedByID: deletedByID,389 }390 sess := x.NewSession()391 defer sess.Close()392 if err := sess.Begin(); err != nil {393 return err394 }395 if _, err := sess.InsertOne(deletedBranch); err != nil {396 return err397 }398 return sess.Commit()399}400// GetDeletedBranches returns all the deleted branches401func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {402 deletedBranches := make([]*DeletedBranch, 0)403 return deletedBranches, x.Where("repo_id = ?", repo.ID).Desc("deleted_unix").Find(&deletedBranches)404}405// GetDeletedBranchByID get a deleted branch by its ID406func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) {407 deletedBranch := &DeletedBranch{ID: ID}408 has, err := x.Get(deletedBranch)409 if err != nil {410 return nil, err411 }412 if !has {413 return nil, nil414 }415 return deletedBranch, nil416}417// RemoveDeletedBranch removes a deleted branch from the database418func (repo *Repository) RemoveDeletedBranch(id int64) (err error) {419 deletedBranch := &DeletedBranch{420 RepoID: repo.ID,421 ID: id,422 }423 sess := x.NewSession()424 defer sess.Close()425 if err = sess.Begin(); err != nil {426 return err427 }428 if affected, err := sess.Delete(deletedBranch); err != nil {429 return err430 } else if affected != 1 {431 return fmt.Errorf("remove deleted branch ID(%v) failed", id)432 }433 return sess.Commit()434}435// LoadUser loads the user that deleted the branch436// When there's no user found it returns a NewGhostUser437func (deletedBranch *DeletedBranch) LoadUser() {438 user, err := GetUserByID(deletedBranch.DeletedByID)439 if err != nil {440 user = NewGhostUser()441 }442 deletedBranch.DeletedBy = user443}444// RemoveDeletedBranch removes all deleted branches445func RemoveDeletedBranch(repoID int64, branch string) error {446 _, err := x.Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))447 return err448}449// RemoveOldDeletedBranches removes old deleted branches450func RemoveOldDeletedBranches(ctx context.Context) {451 // Nothing to do for shutdown or terminate452 log.Trace("Doing: DeletedBranchesCleanup")453 deleteBefore := time.Now().Add(-setting.Cron.DeletedBranchesCleanup.OlderThan)454 _, err := x.Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))...
tun_protect.go
Source:tun_protect.go
...31)32// A list of non-retriable errors:33var (34 // ErrTunProtectNoInterface is returned when VPP tunnel protection was defined without an interface.35 ErrTunProtectNoInterface = errors.New("VPP tunnel protection defined without interface")36 // ErrTunProtectNoSaOut is returned when VPP tunnel protection was defined without outbound SAs.37 ErrTunProtectNoSaOut = errors.New("VPP tunnel protection defined without outbound SAs")38 // ErrTunProtectNoSaIn is returned when VPP tunnel protection was defined without inbound SAs.39 ErrTunProtectNoSaIn = errors.New("VPP tunnel protection defined without inbound SAs")40 // ErrTunProtectUpdateIfMismatch is returned if old and new tunnel interface names are not matching by update operation.41 ErrTunProtectUpdateIfMismatch = errors.New("old/new tunnel interface mismatch")42)43// TunnelProtectDescriptor teaches KVScheduler how to configure VPP IPSec tunnel protections.44type TunnelProtectDescriptor struct {45 // dependencies46 log logging.Logger47 ipSecHandler vppcalls.IPSecVppAPI48}49// NewTunnelProtectDescriptor creates a new instance of the IPSec tunnel protect descriptor.50func NewTunnelProtectDescriptor(ipSecHandler vppcalls.IPSecVppAPI, log logging.PluginLogger) *TunnelProtectDescriptor {51 return &TunnelProtectDescriptor{52 ipSecHandler: ipSecHandler,53 log: log.NewLogger("tun-protect-descriptor"),54 }55}56// GetDescriptor returns a new tunnel protect descriptor suitable for registration with the KVScheduler.57func (d *TunnelProtectDescriptor) GetDescriptor() *adapter.TunProtectDescriptor {58 return &adapter.TunProtectDescriptor{59 Name: TunProtectDescriptorName,60 NBKeyPrefix: ipsec.ModelTunnelProtection.KeyPrefix(),61 ValueTypeName: ipsec.ModelTunnelProtection.ProtoName(),62 KeySelector: ipsec.ModelTunnelProtection.IsKeyValid,63 KeyLabel: ipsec.ModelTunnelProtection.StripKeyPrefix,64 Validate: d.Validate,65 Create: d.Create,66 Update: d.Update,67 Delete: d.Delete,68 Retrieve: d.Retrieve,69 Dependencies: d.Dependencies,70 }71}72// Validate validates VPP tunnel protect configuration.73func (d *TunnelProtectDescriptor) Validate(key string, tp *ipsec.TunnelProtection) error {74 if tp.Interface == "" {75 return kvs.NewInvalidValueError(ErrTunProtectNoInterface, "interface")76 }77 if len(tp.SaOut) == 0 {78 return kvs.NewInvalidValueError(ErrTunProtectNoSaOut, "sa_out")79 }80 if len(tp.SaIn) == 0 {81 return kvs.NewInvalidValueError(ErrTunProtectNoSaIn, "sa_in")82 }83 return nil84}85// Create adds a new IPSec tunnel protection.86func (d *TunnelProtectDescriptor) Create(key string, tp *ipsec.TunnelProtection) (metadata interface{}, err error) {87 return nil, d.ipSecHandler.AddTunnelProtection(tp)88}89// Update updates an existing IPSec tunnel protection.90func (d *TunnelProtectDescriptor) Update(key string, oldTp, newTp *ipsec.TunnelProtection, oldMeta interface{}) (91 metadata interface{}, err error) {92 if oldTp.Interface != newTp.Interface {93 return nil, ErrTunProtectUpdateIfMismatch94 }95 return nil, d.ipSecHandler.AddTunnelProtection(newTp)...
memory_windows.go
Source:memory_windows.go
...8 "unsafe"9 "golang.org/x/sys/windows"10)11var (12 kernel32 = windows.NewLazySystemDLL("kernel32")13 kernel32VirtualAlloc = kernel32.NewProc("VirtualAlloc")14 kernel32VirtualFree = kernel32.NewProc("VirtualFree")15 kernel32VirtualProtect = kernel32.NewProc("VirtualProtect")16 kernel32GetNativeSystemInfo = kernel32.NewProc("GetNativeSystemInfo")17 kernel32FlushInstructionCache = kernel32.NewProc("FlushInstructionCache")18 kernel32GetCurrentProcess = kernel32.NewProc("GetCurrentProcess")19 pageSize uint6420 pageSizeOnce sync.Once21)22// getCurrentProcess returns the current process handle.23func getCurrentProcess() uintptr {24 r, _, _ := kernel32GetCurrentProcess.Call()25 return uintptr(r)26}27// GetPageSize returns the size of a memory page.28func GetPageSize() uint64 {29 pageSizeOnce.Do(func() {30 if kernel32GetNativeSystemInfo.Find() != nil {31 pageSize = 0x100032 }33 info := systemInfo{}34 kernel32GetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&info)))35 pageSize = uint64(info.dwPageSize)36 })37 return pageSize38}39// Memory represents a raw block of memory.40type Memory struct {41 data []byte42 i int6443}44// Alloc allocates memory at addr of size with allocType and protect.45// It returns nil if it fails.46func Alloc(addr, size uint64, allocType, protect int) *Memory {47 r, _, _ := kernel32VirtualAlloc.Call(uintptr(addr), uintptr(size), uintptr(allocType), uintptr(protect))48 if r == 0 {49 return nil50 }51 return Get(uint64(r), size)52}53// Get returns a range of existing memory. If the range is not a block of54// allocated memory, the returned memory will pagefault when accessed.55func Get(addr, size uint64) *Memory {56 m := &Memory{}57 sh := (*reflect.SliceHeader)(unsafe.Pointer(&m.data))58 sh.Data = uintptr(addr)59 sh.Len = int(size)60 sh.Cap = int(size)61 return m62}63// Free frees the block of memory.64func (m *Memory) Free() {65 sh := (*reflect.SliceHeader)(unsafe.Pointer(&m.data))66 kernel32VirtualFree.Call(sh.Data, 0, memRelease)67 m.data = nil68}69// Addr returns the actual address of the memory.70func (m *Memory) Addr() uint64 {71 return uint64((*reflect.SliceHeader)(unsafe.Pointer(&m.data)).Data)72}73// Read implements the io.Reader interface.74func (m *Memory) Read(b []byte) (n int, err error) {75 if m.i >= int64(len(m.data)) {76 return 0, io.EOF77 }78 n = copy(b, m.data[m.i:])79 m.i += int64(n)80 return n, nil81}82// ReadAt implements the io.ReaderAt interface.83func (m *Memory) ReadAt(b []byte, off int64) (n int, err error) {84 if off < 0 {85 return 0, errors.New("negative offset")86 }87 if off >= int64(len(m.data)) {88 return 0, io.EOF89 }90 n = copy(b, m.data[off:])91 if n < len(b) {92 return n, io.EOF93 }94 return n, nil95}96// Write implements the io.Writer interface.97func (m *Memory) Write(b []byte) (n int, err error) {98 if m.i >= int64(len(m.data)) {99 return 0, io.ErrShortWrite100 }101 n = copy(m.data[m.i:], b)102 if kernel32FlushInstructionCache.Find() == nil {103 kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[m.i])), uintptr(n))104 }105 m.i += int64(n)106 return n, nil107}108// WriteAt implements the io.WriterAt interface.109func (m *Memory) WriteAt(b []byte, off int64) (n int, err error) {110 if off < 0 {111 return 0, errors.New("negative offset")112 }113 if off >= int64(len(m.data)) {114 return 0, io.ErrShortWrite115 }116 n = copy(m.data[off:], b)117 if kernel32FlushInstructionCache.Find() == nil {118 kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[off])), uintptr(n))119 }120 if n < len(b) {121 return n, io.ErrShortWrite122 }123 return n, nil124}125// Seek implements the io.Seeker interface.126func (m *Memory) Seek(offset int64, whence int) (int64, error) {127 var n int64128 switch whence {129 case io.SeekStart:130 n = offset131 case io.SeekCurrent:132 n = m.i + offset133 case io.SeekEnd:134 n = int64(len(m.data)) + offset135 default:136 return 0, errors.New("invalid whence")137 }138 if n < 0 {139 return 0, errors.New("negative position")140 }141 m.i = n142 return n, nil143}144// Clear sets all bytes in the memory block to zero.145func (m *Memory) Clear() {146 for i := range m.data {147 m.data[i] = 0148 }149 if kernel32FlushInstructionCache.Find() == nil {150 kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[0])), uintptr(len(m.data)))151 }152}153// Protect changes the memory protection for a range of memory....
New
Using AI Code Generation
1import "fmt"2func main() {3 p := protect.New(10)4 fmt.Println(p.Get())5 p.Set(20)6 fmt.Println(p.Get())7}8import "fmt"9func main() {10 p := protect.New(10)11 fmt.Println(p.Get())12 p.Set(20)13 fmt.Println(p.Get())14}15import "fmt"16func main() {17 p := protect.New(10)18 fmt.Println(p.Get())19 p.Set(20)20 fmt.Println(p.Get())21}22import "fmt"23func main() {24 p := protect.New(10)25 fmt.Println(p.Get())26 p.Set(20)27 fmt.Println(p.Get())28}29import "fmt"30func main() {31 p := protect.New(10)32 fmt.Println(p.Get())33 p.Set(20)34 fmt.Println(p.Get())35}36import "fmt"37func main() {38 p := protect.New(10)39 fmt.Println(p.Get())40 p.Set(20)41 fmt.Println(p.Get())42}43import "fmt"44func main() {45 p := protect.New(10)46 fmt.Println(p.Get())47 p.Set(20)48 fmt.Println(p.Get())49}50import "fmt"51func main() {52 p := protect.New(10)53 fmt.Println(p.Get())54 p.Set(20)55 fmt.Println(p.Get())56}57import "fmt"58func main() {59 p := protect.New(10)60 fmt.Println(p.Get())61 p.Set(20)62 fmt.Println(p.Get())63}
New
Using AI Code Generation
1import (2type protect struct {3}4func New(x, y float64) *protect {5 return &protect{x, y}6}7func (p *protect) Abs() float64 {8 return math.Sqrt(p.x*p.x + p.y*p.y)9}10func main() {11 p := New(3, 4)12 fmt.Println(p.Abs())13}14import (15type protect struct {16}17func New(x, y float64) *protect {18 return &protect{x, y}19}20func (p *protect) Abs() float64 {21 return math.Sqrt(p.x*p.x + p.y*p.y)22}23func main() {24 p := New(3, 4)25 fmt.Println(p.Abs())26}27import (28type protect struct {29}30func New(x, y float64) *protect {31 return &protect{x, y}32}33func (p *protect) Abs() float64 {34 return math.Sqrt(p.x*p.x + p.y*p.y)35}36func main() {37 p := New(3, 4)38 fmt.Println(p.Abs())39}40import (41type protect struct {42}43func New(x, y float64) *protect {44 return &protect{x, y}45}46func (p *protect) Abs() float64 {47 return math.Sqrt(p.x*p.x + p.y*p.y)48}49func main() {50 p := New(3, 4)51 fmt.Println(p.Abs())52}53import (54type protect struct {55}56func New(x, y float64) *protect {57 return &protect{x, y}58}59func (p *protect) Abs() float
New
Using AI Code Generation
1func main() {2 fmt.Println(p.String())3}4func main() {5 fmt.Println(p.String())6}7func main() {8 fmt.Println(p.String())9}10func main() {11 fmt.Println(p.String())12}13func main() {14 fmt.Println(p.String())15}16func main() {17 fmt.Println(p.String())18}19func main() {20 fmt.Println(p.String())21}22func main() {23 fmt.Println(p.String())24}25func main() {26 fmt.Println(p.String())27}28func main() {29 fmt.Println(p.String())30}31func main() {32 fmt.Println(p.String())33}34func main() {35 fmt.Println(p.String())36}37func main() {
New
Using AI Code Generation
1import (2func main() {3 fmt.Println(protector.New())4}5import (6func main() {7 fmt.Println(protector.New())8}
New
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4 p := protect.New(10)5 fmt.Println(p.Get())6}7type Protect struct {8}9func New(i int) *Protect {10 return &Protect{i}11}12func (p *Protect) Get() int {13}14import "testing"15func TestGet(t *testing.T) {16 p := New(10)17 if p.Get() != 10 {18 t.Error("Expected 10, got ", p.Get())19 }20}
New
Using AI Code Generation
1import "fmt"2type protect struct {3}4func (p *protect) setValue(val int) {5}6func (p *protect) getValue() int {7}8func New() *protect {9return &protect{}10}11func main() {12p := New()13p.setValue(10)14fmt.Println(p.getValue())15}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!